Core Java Questions Flashcards

1
Q

What if the main method is declared as private?

A

The program compiles properly but at runtime it will give “Main method not public.” message.

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

What is meant by pass by reference and pass by value in Java?

A

Pass by reference means, passing the address itself rather than passing the value. Pass by value means passing a copy of the value.

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

If you’re overriding the method equals() of an object, which other method you might also consider?

A

hashCode()

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

What is Byte Code?

Or

What gives java it’s “write once and run anywhere” nature?

A

All Java programs are compiled into class files that contain bytecodes. These byte codes can be run in any platform and hence java is said to be platform independent.

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

Expain the reason for each keyword of public static void main(String args[])?

A
  • *public** – main(..) is the first method called by java environment when a program is executed so it has to accessible from java environment. Hence the access specifier has to be public.
  • *static** : Java environment should be able to call this method without creating an instance of the class , so this method must be declared as static.
  • *void** : main does not return anything so the return type must be void

The argument String indicates the argument type which is given at the command line and arg is an array for string given during command line.

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

What are the differences between == and .equals()?

A

The == operator compares two objects to determine if they are the same object in memory i.e. present in the same memory location. It is possible for two String objects to have the same value, but located in different areas of memory.

== compares references while .equals compares contents. The method public boolean equals(Object obj) is provided by the Object class and can be overridden. The default implementation returns true only if the object is compared with itself, which is equivalent to the equality operator == being used to compare aliases to the object. String, BitSet, Date, and File override the equals() method. For two String objects, value equality means that they contain the same character sequence. For the Wrapper classes, value equality means that the primitive values are equal.

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

What if the static modifier is removed from the signature of the main method?

Or

What if I do not provide the String array as the argument to the method?

A

Program compiles. But at runtime throws an error “NoSuchMethodError”.

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

Why oracle Type 4 driver is named as oracle thin driver?

A

Oracle provides a Type 4 JDBC driver, referred to as the Oracle “thin” driver. This driver includes its own implementation of a TCP/IP version of Oracle’s Net8 written entirely in Java, so it is platform independent, can be downloaded to a browser at runtime, and does not require any Oracle software on the client side. This driver requires a TCP/IP listener on the server side, and the client connection string uses the TCP/IP port address, not the TNSNAMES entry for the database name.

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

What is final, finalize() and finally?

A
  • *final** – declare constant
  • *finally** – handles exception
  • *finalize** – helps in garbage collection

Variables defined in an interface are implicitly final. A final class can’t be extended i.e., final class may not be subclassed. This is done for security reasons with basic classes like String and Integer. It also allows the compiler to make some optimizations, and makes thread safety a little easier to achieve. A final method can’t be overridden when its class is inherited. You can’t change value of a final variable (is a constant). finalize() method is used just before an object is destroyed and garbage collected. finally, a key word used in exception handling and will be executed whether or not an exception is thrown. For example, closing of open connections is done in the finally method.

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

What is the Java API?

A

The Java API is a large collection of ready-made software components that provide many useful capabilities, such as graphical user interface (GUI) widgets.

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

What is the GregorianCalendar class?

A

The GregorianCalendar provides support for traditional Western calendars.

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

What is the ResourceBundle class?

A

The ResourceBundle class is used to store locale-specific resources that can be loaded by a program to tailor the program’s appearance to the particular locale in which it is being run.

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

Why there are no global variables in Java?

A

Global variables are globally accessible. Java does not support globally accessible variables due to following reasons:

  • The global variables breaks the referential transparency
  • Global variables create collisions in namespace.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

The valueOf() function of Integer class is is used to convert string to Number. Here is the code example:

A

The valueOf() function of Integer class is is used to convert string to Number. Here is the code example:

//code below

1 String numString = “1000″;

2 int id = Integer.valueOf(numString).intValue();

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

Encapsulation

A

Encapsulation provides objects with the ability to hide their internal characteristics and behavior. Each object provides a number of methods, which can be accessed by other objects and change its internal data. In Java, there are three access modifiers: public, private and protected.

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

Polymorphism

A

Polymorphism is the ability of programming languages to present the same interface for differing underlying data types. A polymorphic type is a type whose operations can also be applied to values of some other type.

17
Q

Inheritance

A

Inheritance provides an object with the ability to acquire the fields and methods of another class, called base class. Inheritance provides re-usability of code and can be used to add additional features to an existing class, without modifying it.

18
Q

Abstraction

A

Abstraction is the process of separating ideas from specific instances and thus, develop classes in terms of their own functionality, instead of their implementation details. Java supports the creation and existence of abstract classes that expose interfaces, without including the actual implementation of all methods. The abstraction technique aims to separate the implementation details of a class from its behavior.

19
Q

What is Autoboxing and Unboxing ?

A

Autoboxing is the automatic conversion made by the Java compiler between the primitive types and their corresponding object wrapper classes. For example, the compiler converts an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this operation is called unboxing.

20
Q

What is Function Overriding and Overloading in Java ?

A

Method overloading in Java occurs when two or more methods in the same class have the exact same name, but different parameters. On the other hand, method overriding is defined as the case when a child class redefines the same method as a parent class. Overridden methods must have the same name, argument list, and return type. The overriding method may not limit the access of the method it overrides.

21
Q

What is a Constructor, Constructor Overloading in Java and Copy-Constructor ?

A

A constructor gets invoked when a new object is created. Every class has a constructor. In case the programmer does not provide a constructor for a class, the Java compiler (Javac) creates a default constructor for that class. The constructor overloading is similar to method overloading in Java. Different constructors can be created for a single class. Each constructor must have its own unique parameter list.

22
Q

Does Java support multiple inheritance ?

A

No, Java does not support multiple inheritance. Each class is able to extend only on one class, but is able to implement more than one interfaces.

23
Q

What is the difference between an Interface and an Abstract class ?

A
  • All methods in an interface are implicitly abstract. On the other hand, an abstract class may contain both abstract and non-abstract methods.
  • A class may implement a number of Interfaces, but can extend only one abstract class.
  • In order for a class to implement an interface, it must implement all its declared methods. However, a class may not implement all declared methods of an abstract class. Though, in this case, the sub-class must also be declared as abstract.
  • Abstract classes can implement interfaces without even providing the implementation of interface methods.
  • Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.
  • Members of a Java interface are public by default. A member of an abstract class can either be private, protected or public.
  • An interface is absolutely abstract and cannot be instantiated. An abstract class also cannot be instantiated, but can be invoked if it contains a main method.
24
Q

What are pass by reference and pass by value ?

A

When an object is passed by value, this means that a copy of the object is passed. Thus, even if changes are made to that object, it doesn’t affect the original value. When an object is passed by reference, this means that the actual object is not passed, rather a reference of the object is passed. Thus, any changes made by the external method, are also reflected in all places.

25
Q

What is the difference between processes and threads ?

A

A process is an execution of a program, while a Thread is a single execution sequence within a process. A process can contain multiple threads. A Thread is sometimes called a lightweight process.

26
Q

Explain different ways of creating a thread. Which one would you prefer and why ?

A

There are three ways that can be used in order for a Thread to be created:

  • A class may extend the Thread class.
  • A class may implement the Runnable interface.
  • An application can use the Executor framework, in order to create a thread pool.

The Runnable interface is preferred, as it does not require an object to inherit the Thread class. In case your application design requires multiple inheritance, only interfaces can help you. Also, the thread pool is very efficient and can be implemented and used very easily.

27
Q

Explain the available thread states in a high-level.

A
  • NEW: The thread becomes ready to run, but does not necessarily start running immediately.
  • RUNNABLE: The Java Virtual Machine (JVM) is actively executing the thread’s code.
  • BLOCKED: The thread is in a blocked state while waiting for a monitor lock.
  • WAITING: The thread waits for another thread to perform a particular action.
  • TIMED_WAITING: The thread waits for another thread to perform a particular action up to a specified waiting time.
  • TERMINATED: The thread has finished its execution
28
Q

What is the difference between a synchronized method and a synchronized block ?

A

In Java programming, each object has a lock. A thread can acquire the lock for an object by using the synchronized keyword. The synchronized keyword can be applied in a method level (coarse grained lock) or block level of code (fine grained lock).

29
Q

How does thread synchronization occurs inside a monitor ? What levels of synchronization can you apply ?

A

The JVM uses locks in conjunction with monitors. A monitor is basically a guardian that watches over a sequence of synchronized code and ensuring that only one thread at a time executes a synchronized piece of code. Each monitor is associated with an object reference. The thread is not allowed to execute the code until it obtains the lock.

30
Q

What’s a deadlock ?

A

A condition that occurs when two processes are waiting for each other to complete, before proceeding. The result is that both processes wait endlessly.

31
Q

How do you ensure that N threads can access N resources without deadlock ?

A

A very simple way to avoid deadlock while using N threads is to impose an ordering on the locks and force each thread to follow that ordering. Thus, if all threads lock and unlock the mutexes in the same order, no deadlocks can arise.

32
Q

What are the basic interfaces of Java Collections Framework ?

A
  • Collection, which represents a group of objects known as its elements.
  • Set, which is a collection that cannot contain duplicate elements.
  • List, which is an ordered collection and can contain duplicate elements.
  • Map, which is an object that maps keys to values and cannot contain duplicate keys.
33
Q

Why Collection doesn’t extend Cloneable and Serializable interfaces ?

A

The Collection interface specifies groups of objects known as elements. Each concrete implementation of a Collection can choose its own way of how to maintain and order its elements. Some collections allow duplicate keys, while some other collections don’t. The semantics and the implications of either cloning or serialization come into play when dealing with actual implementations. Thus, the concrete implementations of collections should decide how they can be cloned or serialized.

34
Q

What is an Iterator ?

A

The Iterator interface provides a number of methods that are able to iterate over any Collection. Each Java Collection contains the iterator method that returns an Iterator instance. Iterators are capable of removing elements from the underlying collection during the iteration.

35
Q

What differences exist between Iterator and ListIterator ?

A
  • An Iterator can be used to traverse the Set and List collections, while the ListIterator can be used to iterate only overLists.
  • The Iterator can traverse a collection only in forward direction, while the ListIterator can traverse a List in both directions.
  • The ListIterator implements the Iterator interface and contains extra functionality, such as adding an element, replacing an element, getting the index position for previous and next elements, etc