Mock 3 Recently Asked Questions Flashcards

1
Q

What are the restrictions of Arrays?

A

• 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to convert String to an int?

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to convert primitive int to a String?

A
  • 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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between = and == operators?

A

• = 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the difference between == operator and equals() method?

A
• == 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can we use primitive data types as objects?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Can we have abstract class without having any abstract method in it?

A
• 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can we have two methods in a class with the same name?

A

• 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Is it possible to have static methods in an Interface?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Can we declare the main() method as private?

A

• 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How we can execute any code even before main() method?

A

• 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Can main() method return any data in Java?

A

• No, main() method cannot return any data as it is a void type method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Is it possible to make constructor final?

A

• No, we cannot declare constructor as final and we cannot declare constructor as static

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How objects of a class are created if no constructor is defined in the class?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Is it possible to call a constructor from another constructor’s body?

A
Yes. If a class has multiple constructors, then it is possible to call one constructor from the body of another one 
using this()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Can we use a default constructor of a class even if a custom constructor is created?

A
  • Java provides a default no argument constructor if we do not create any custom constructor
  • However, once we create a custom constructor, default constructor cannot be used anymore
  • If we still want to create objects with default constructor, we need to explicitly define it in the class
17
Q

Is it possible to create an object of any Interface?

A
  • They may ask same question for abstract classes as well
  • NO, we cannot instantiate both abstract classes and interfaces meaning we cannot create objects for them

• REMEMBER:

o Both abstract classes and interfaces are created to be used by other classes with inheritance 
o Abstract classes and interfaces are created to be used by many similar subclasses and provides
some common fields and methods to those subclasses and increases code reusability
o Abstract class can be parent to many other child classes but can extend to only one parent (single 
inheritance). To be able to have additional features, Java introduces interfaces and multiple 
inheritance is possible with interfaces. One interface can extend to many other interfaces while one 
class can implement many other interfaces
18
Q

Can one interface implement another interface in java?

A

No, one interface cannot implement another interface. It can extend it using extends keyword

19
Q

Can a class be a super class and a sub-class at the same time?

A

Yes, a class can be a super class for another class and a sub-class to another one at the same time

20
Q

Can a class in Java be inherited from more than one class?

A

Yes, one class can be base (parent) to multiple classes, but one child cannot extend to multiple base classes

21
Q

How can we restrict inheritance for a class so that no class can extend to it?

A
• REMEMBER: when we make a class final, it means it cannot be inherited
• So, if we want a class not to be extended further by any class, we can use the keyword final with the class 
name, and this will restrict it to be a parent class
22
Q

What is the base class for all the classes in Java?

A
  • Base class means super class or parent class as well
  • Object class is the base class for all of the other classes in Java
  • Object class is coming from java.lang package
23
Q

Can you list down some of important method from object class?

A
  • equals() - It compares the object references
  • toString() - Provides String representation of the object
  • finalize() - This method is called when object is being garbage collected
24
Q

What are the types of comments in Java?

A
  • Single line comment with //This is a single line comment

* Multiple line comments /This is multiple line comments/

25
Q

Why String is immutable?

A
• String class is immutable in Java
• It means unable to be changed or unchanging over time, so String is unchangeable or unmodifiable in Java
• When we create a String as String s = “Text”; It will be stored in String Pool in the heap and this value might 
be referenced by some other variables as well. And if you were able to change the value, then it would 
change for all variables referencing to the same value. To prevent this, String is immutable
• Another reason is being thread-safe (synchronized). String and all other immutable object are thread-safe 
by default, so you don’t need to explicitly implement synchronization for them
• It is also safe to use considering security as the value of String will not be changed
26
Q

What are two different ways to call garbage collector explicitly?

A

• Garbage collection is destroying objects that lose references and it happens implicitly in Java
• BUT, we can do garbage collection explicitly using System.gc() or Runtime.getRuntime().gc() methods
• These methods can be used as a hint to the JVM, in order to start a garbage collection. However, this it is up
to the Java Virtual Machine (JVM) to start the garbage collection immediately or later in time

27
Q

Can we have try block without catch block in Java?

A
  • Yes, we can have try block without catch block by using finally block
  • You can use try with either catch or finally block or with both
28
Q

How can you generate random numbers in Java?

A
• We can use Math.random() to generate random numbers
• Also, Random class in package java.util can be used to generate Random numbers
NOTE: Some third-party libraries can be added to project and be used to generate random data including 
numbers
29
Q

What are Java packages? What’s the significance of packages?

A

• Package is a collection of classes and interfaces which are bundled together as they are related to each
other
• Use of packages helps programmers to modularize the code and group the code for proper re-use
• Once code has been packaged in packages, it can be imported in other classes and used

30
Q

What is the purpose of using break in each case of switch statement?

A

• break is used after each case (except the default case) in a switch, so that code breaks after the valid case
and does not flow in the proceeding cases too
• If break is not used after each case, all cases after the valid case will get executed resulting in wrong results

31
Q

Can a variable be local and static at the same time?

A
• No, a variable cannot be static as well as local at the same time
• Defining a local variable as static gives a compilation error
NOTE: Instance variables can be static, and it means it is shared by all objects of the class and belongs to the 
class anymore
32
Q

Is Java 100% Object-Oriented Programming Language?

A
  • Java is an Object-Oriented Programming Language

* BUT Java has primitive data types, and that is why Java is not 100% Object-Oriented

33
Q

What is Enum in Java?

A
• An enum is a special "class" that represents a group of constants (unchangeable variables, 
like final variables).
• To create an enum, use the enum keyword (instead of class or interface), and separate the constants with a 
comma
• Note that they should be in uppercase letters
• Enum is useful to use for values that will not change like months, days, colors, etc.
34
Q

What is multi-threading in Java?

A

• Multi-threading is a programming concept to run multiple tasks in a concurrent manner within a single
program
• Threads are used for that and they share same process stack and running in parallel for different tasks
• It helps in performance improvement of any program