Mock 3 Recently Asked Questions Flashcards
What are the restrictions of Arrays?
• This question could be asked as “Why we need Collections in Java” as well
• Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each
value
• By using an Array, we can store many similar items/elements in one variable
• Arrays can be used with both primitive and reference data types while all other collections can only be used
with reference types (objects)
• HOWEVER, Array has some restriction and that’s why we need to have a good knowledge of using
collections based on the need
• Array does not provide methods to manipulate data as other collections
How to convert String to an int?
- This question could be asked for all of the primitives except char.
- We can convert String to related primitive by using their Wrapper classes
- valueOf() and parseType() methods can be used for the conversion
EXAMPLE: String str = “235”; int number1 = Integer.valueOf(str); or int number2 = Integer.parseInt(str); double d1 = Double.valueOf(str); or double d2 = Double.parseDouble(str);
How to convert primitive int to a String?
- This could be asked for all other primitive data types
- We can convert any primitive data type to a String
- Using + (concatenation), will convert any data type to a String
- Or String.valueOf(variable) method can be used
EXAMPLE:
int age = 45;
String stringAge1 = age + “”; // “” is known as empty String
String stringAge2 = String.valueOf(age);
What is the difference between = and == operators?
• = is the assignment operator and used to assign values into variables.
EXAMPLE:
int age = 45; // 45 is assigned to the age variable
• == is one of the relational operators and it is used to find if the value on both sides of the operator are equal to each
other. It will return a boolean, either true or false based on the comparison
EXAMPLE:
5 == 5 // this comparison will return true and it can be assigned to a boolean as below
boolean equal = (5 == 5); // in this statement, equal variable has value of true as 5 equals to 5
What is the difference between == operator and equals() method?
• == is one of the relational operators and it is used to find if the values on both sides of the operator are equal to each other. It will return a boolean, either true or false based on the comparison • == is mostly used with primitive data types and should not be used with reference types (objects) • When == is used with reference types, it compares their location in the memory and each object created with new keyword will have a unique location. That’s why, although the object variable values are same, == will return a false since it compares their location and locations are obviously different. • equals() is mostly known to be used with String objects but String is not the only class that has equals() method. Wrapper classes, Arrays, ArrayList, Vector and other collections also has equals() method and this method is used to check 2 objects of same data type has same value or not and return a boolean as true or false
EXAMPLES
String str1 = “Tech”;
String str2 = “Global”;
System.out.println(str1.equals(str2)); // this statement will print false as 2 String object values are not equal
How can we use primitive data types as objects?
- Primitive data types like int can be handled as objects by the use of their respective wrapper classes
- For example, Integer is a wrapper class for primitive data type int
- We can apply different methods to a wrapper class, just like any other object
Can we have abstract class without having any abstract method in it?
• Yes, we can have abstract class without having any abstract method • REMEMBER, abstract class can have both abstract and non-abstract methods and it may not have one of these
Can we have two methods in a class with the same name?
• Yes, we can define two methods in a class with the same name but with different number/type of
parameters
• This is known as method overloading
Is it possible to have static methods in an Interface?
- Methods are by default public abstract in the Interface
- BUT we can have static methods in an Interface after Java 8
- We can also have default methods that has body in an Interface
Can we declare the main() method as private?
• main() method must be public static in order to run any application correctly
• If main method is declared as private, programmer will not get any compilation error but, it will not get
executed
How we can execute any code even before main() method?
• We know that starting point of an application in Java is main() method and when we run a program JVM
first finds the main() method and executes statements in it
• If we want to execute any statements before even creation of objects, we can use a static block of code in
the class
• Any statements inside the static block of code will get executed once at the time of loading the class even
before creation of objects in the main method
Can main() method return any data in Java?
• No, main() method cannot return any data as it is a void type method
Is it possible to make constructor final?
• No, we cannot declare constructor as final and we cannot declare constructor as static
How objects of a class are created if no constructor is defined in the class?
- REMEMBER: Java always provides a default constructor even if we do not create a custom one
- Objects are getting created successfully with default constructor
- This constructor has no parameters
Is it possible to call a constructor from another constructor’s body?
Yes. If a class has multiple constructors, then it is possible to call one constructor from the body of another one using this()