Interview questions 2 Flashcards

1
Q
  1. Can you overload the main method?
A

Absolutely. However, the JVM will only use the standard main(String[] args) when launching. the standard entry point must be a method named main , but it must also have the public and static modifiers, along with the void return type; finally, the parameter can be written with the type String[] or the type String…. Any other method named main will not be used as an entry point, and is simply just an overload.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. What is a marker interface?
A

A Marker interface is an interface with no methods on it, used only as an indicator. Serializable and Clonable are examples in the JDK.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. Can a constructor be final?
A

Nope. for more detail, consider this: final is used on methods to prevent overriding; a constructor is not a method, and is not inherited; therefore, final makes no sense on a construct

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. What does applying final actually do?
A

Applying final to a variable means the variable cannot be changed.

Applying final to method means they can be overridden.

Applying final to a class means the class cannot be overridden

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. What is an anonymous inner class?
A

It is possible to create the implementation of an interface in line. Syntactically, it looks like we are instantiating a new instance of an interface.It is called anonymous because it cannot be referenced elsewhere. It literally has no name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. What is casting in Java?
A

Casting is the act of turning one object into another. We can refer to an Integer as an Integer, or upcast it to an Object. It is an upcast as it is higher in the inheritance hierarchy. Alternatively, if we cast the other way from an Object to an Integer, we are downcasting. We must be careful when casting as, if the object cast is invalid it will throw a ClassCastException.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. Can we override an overloaded method?
A

Yes! Of course. They are two orthogonal concepts.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. What is a transient variable?
A

When a variable is marked transient it will not be serialized.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. What is the difference between while and do while?
A

A while loop is not guaranteed to run; if the condition is false then the code in the loop will not be executed.
A do while is guaranteed to execute once.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. What does the % operator do?
A

% is the modulus operator. It will give the remainder of a division. For example, in 11 / 2, the result is 5. 11%2 will provide the remainder, which is 1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. What is the Big O of object retrieval in a hashmap and why?
A

In the best case of no clashes it will be O(1). The hashing function will result in direct access to the element, irrelevant of how many other objects are in the map. However, in the worst case it could be O(n), as if the hashing function results in all the objects landing in the same bucket it will be the same complexity as linked list (which is the backing structure; see Java Data Structures Interview Questions for more).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. What is the result of the following statement?
    1
    int[] ints = {1, 2, 3, 4, 5};
    2
    System.out.println((ints.length/2) * (ints.length + 1));
A

This outputs 12. Because array.length is an integer, ints.length/2 = 2, not 2.5.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. What is the difference between the JVM, JRE and JDK?
A

JVM is the Java Virtual Machine. All java applications execute on the Java Virtual Machine. (Read more in JVM and Garbage Collection Interview Questions: The Beginners Guide).

The JRE is the Java Runtime Environment. The JRE contains the JVM, along with supporting files such as libraries.

The JDK is the Java Development Kit and contains the JRE (and JVM) along with the tools developers need to build and create applications, such as a compiler.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. What is a constructor? Why is it different from other methods?
A
A constructor is used to create an object.  It does not specify a return type (as the return type is implied; the constructor for MyObject will return a MyObject type). Constructor must be called via the new keyword, whereas regular methods will be called on an existing object (or static methods on a class). Constructors must have the same name as the class.  A default constructor is created by the JVM if one is not coded. If a class is a subclass then the first line must be to call super() to initialise the variables etc. inherited from the parent class.
it is important to remember that constructors are not methods.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. Does importing a package, e.g. import com.cjiq., also import it’s subpackages e.g. com.cjiq.hello.
A

No, They must be imported explicitly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  1. Explain pass by reference and pass by value
A

In pass by reference, a reference to the object is passed around, not the actual object.

In pass by value, a copy of the actual object is passed around. This means it cannot be modified by the method it is passed to.

17
Q
  1. Is Java Pass by reference or pass by value?
A

Java is always pass by value. However, when passing an object to a method, the reference is passed by value. This means the same object is referenced by both the method and the caller despite being pass by value.

18
Q
  1. Are Java variables initialized with default values?
A

Static and Instance variables are given default values (e.g. false, 0, and null for Objects. Local variables are not, and attempting to access them before initialization will result in a compiler error.

19
Q
  1. What is Autoboxing?
A

Autoboxing is the process of the JVM automatically converting between a primitive type and it’s companion Object equivalent, e.g. int and Integer, boolean and Boolean. We should be careful when relying on this as it can have huge performance impacts if not used correctly as well as causing some very subtle bugs.

20
Q
  1. What access modifiers can I have on a class?
A
On a top level class, only public or default. 
Marking a class private or protected will result in a compiler error.  Inner classes can be private and static.