Wednesday 2 August 2017





1)What do you know about Java?


A)Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.



2)What are the supported platforms by Java Programming Language?


A)Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX/Linux like HP-Unix, Sun Solaris, Redhat Linux, Ubuntu, CentOS, etc.


3)  Why is Java called the ‘Platform Independent Programming Language’?


A) Platform independence means that execution of your program does not depend on a type of operating system(it could be any: Linux, windows, Mac ..etc). So compile code only once and run it on any System (In C/C++, we need to compile the code for every machine on which we run it). Java is both compiler(javac) and interpreter(JVM) based language. Your java source code is first compiled into byte code using javac compiler. This byte code can be easily converted to equivalent machine code using JVM. JVM(Java Virtual Machine) is available in all operating systems we install. Hence, byte code generated by javac is universal and can be converted to machine code on any operating system, this is the reason why java is platform independent.


4) What are the various access specifiers for Java classes?

A) In Java, access specifiers are the keywords used before a class name which defines the access scope. The types of access specifiers for classes are:
1. Public: Class, Method, Field is accessible from anywhere.
2. Protected: Method, Field can be accessed from the same class to which they belong or from the sub-classes, and from the class of the same package, but not from outside.
3. Default: Method, Field, class can be accessed only from the same package and not from outside of its native package.
4. Private: Method, Field can be accessed from the same class to which they belong.


5) List any five features of Java?

A) Some features include Object Oriented, Platform Independent, Robust, Interpreted, Multithreaded


6)Why is Java Architectural Neutral?

A) Its compiler generates an architecture-neutral object file format, which makes the compiled code to be executable on many processors, with the presence of Java runtime system.

7) What’s the purpose of Static methods and static variables?

A) When there is a requirement to share a method or a variable between multiple objects of a class instead of creating separate copies for each object, we use static keyword to make a method or variable shared for all objects

8) How Java enabled High Performance?

A) Java uses Just-In-Time compiler to enable high performance. Just-In-Time compiler is a program that turns Java bytecode, which is a program that contains instructions that must be interpreted into instructions that can be sent directly to the processor.

9) What is data encapsulation and what’s its significance?

A) Encapsulation is a concept in Object Oriented Programming for combining properties and methods in a single unit.Encapsulation helps programmers to follow a modular approach for software development as each object has its own set of methods and variables and serves its functions independent of other objects. Encapsulation also serves data hiding purpose.


10) List two Java IDE’s?

A) Netbeans, Eclipse, etc.


11) Why multiple inheritance is not supported in java?

A) Java supports multiple inheritance but not through classes, it supports only through its interfaces. The reason for not supporting multiple inheritance is to avoid the conflict and complexity arise due to it and keep Java a Simple Object Oriented Language. If we recall this in C++, there is a special case of multiple inheritance where you have a multiple inheritance with two classes which have methods in conflicts. So, Java developers decided to avoid such conflicts and didn’t allow multiple inheritance through classes at all.


12) Can a top level class be private or protected?


A) Top level classes in java can’t be private or protected, but inner classes in java can. The reason for not making a top level class as private is very obvious because nobody can see a private class and thus they cannot use it. Declaring a class as protected also doesn’t make any sense. The only difference between default visibility and protected visibility is that we can use it in any package by inheriting it. Since in Java there is no such concept of package inheritance, defining a class as protected is no different from the default.

13) Define Class?

A) A class is a blue print from which individual objects are created. A class can contain fields and methods to describe the behavior of an object.

14) What do you mean by Object?

A) An object is a runtime entity and its state is stored in fields and behavior is shown via methods. Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication.


15) What kind of variables a class can consist of?

A) A class consist of Local variable, instance variables and class variables.


16) What is a Local Variable?

A) Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and it will be destroyed when the method has completed.

17) What is an Instance Variable?

A) Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded.

18) What is a Class Variable?

A) These are variables declared with in a class, outside any method, with the static keyword.

19) What is a Singleton Class? 

A) A singleton class in java can have only one instance and hence all its methods and variables belong to just one instance. Singleton class concept is useful for the situations when there is a need to limit the number of objects for a class.

20) What are Loops in Java? What are three types of loops?

A) Looping is used in programming to execute a statement or a block of statement repeatedly. There are three types of loops in Java:

1) For Loop:
For loops are used in java to execute statements repeatedly for a given number of times. For loops are used when the number of times to execute the statements is known to the programmer.

2) While Loop:
While loop is used when certain statements need to be executed repeatedly until a condition is fulfilled. In while loops, the condition is checked first before execution of statements.

3) Do While Loop:
Do While Loop is same as While loop with the only difference that condition is checked after execution of the block of statements. Hence in case of doing while loop, statements are executed at least once.

21) What is an Infinite Loop?

A) An infinite loop runs without any condition and runs infinitely. An infinite loop can be broken by defining any breaking logic in the body of the statement blocks.

22) What is the difference between Continue and Break statement?

A)Break and Continue are two important keywords used in Loops. When a break keyword is used in a loop, the loop is broken instantly while when continue keyword is used, the current iteration is broken and the loop continues with next iteration.


23) Can a class have multiple constructors?

A) Yes, a class can have multiple constructors with different parameters. Which constructor gets used for object creation depends on the arguments passed while creating the objects.


24) When parseInt() method can be used?

A) This method is used to get the primitive data type of a certain String.

25)What is the difference between double and float variables in Java?

A) In Java, float takes 4 bytes in memory while Double takes 8 bytes in memory. Float is single precision floating point decimal number while Double is double precision decimal number.

26) What is Final Keyword in Java? Give an example?

A) In java, a constant is declared using the keyword Final. Value can be assigned only once and after assignment, the value of a constant can’t be changed.
In below example, a constant with the name const_val is declared and assigned a value:
Private Final int const_val=100
When a method is declared as final, it can NOT  be overridden by the subclasses.This method is faster than any other method because they are resolved at complied time.

When a class is declared as final, it cannot be subclassed. Example String, Integer, and other wrapper classes.



27) When is the super keyword used?
A) super keyword is used to refer:
  • immediate parent class constructor
  • immediate parent class variable
  • immediate parent class method.


28) What is a ternary operator?

A) A Ternary operator also called conditional operator is used to decide which value to assign to a variable based on a Boolean value evaluation. It’s denoted as?

29) What is the difference between StringBuffer and String?

A) A string is an Immutable class, i.e. you can not modify its content once created. While StringBuffer is a mutable class, means you can change its content later. Whenever we alter the content of String object, it creates a new string and refers to that, it does not modify the existing one. This is the reason that the performance with StringBuffer is better than with String.

30) What is an Exception?

A) An exception is a problem that arises during the execution of a program. Exceptions are caught by handlers positioned along the thread's method invocation stack.


31) What do you mean by Checked Exceptions?

A) It is an exception that is typically a user error or a problem that cannot be foreseen by the programmer. For example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation.

32) Explain Runtime Exceptions?

A)It is an exception that occurs that probably could have been avoided by the programmer. As opposed to checked exceptions, runtime exceptions are ignored at the time of compilation

33) When throws keyword is used?

A) If a method does not handle a checked exception, the method must declare it using the throws keyword. The throws keyword appears at the end of a method's signature.

34) When throw keyword is used?

A) An exception can be thrown, either a newly instantiated one or an exception that you just caught, by using throw keyword.


35) Define Packages in Java?

A) A Package can be defined as a grouping of related types(classes, interfaces, enumerations, and annotations ) providing access protection and name space management.


36) What are the two ways in which Thread can be created?

A) A thread can be created by implementing Runnable interface, extending the Thread class.


37) Explain garbage collection in Java?

A) It uses garbage collection to free the memory. By cleaning those objects that are no longer referenced by any of the programs.

38) Define immutable object?

A) An immutable object can’t be changed once it is created.


39) Explain Set Interface?

A) It is a collection of element which cannot contain duplicate elements. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.

40) Explain TreeSet?

A) It is a Set implemented when we want elements in a sorted order.

41) What is the difference between Overloading and Overriding?

A) Method overloading increases the readability of the program. Method overriding provides the specific implementation of the method that is already provided by its super class parameter must be different in the case of overloading, the parameter must be same in the case of overriding.

42) What are the ways in which a thread can enter the waiting state?

A) A thread can enter the waiting state by invoking its sleep() method, by blocking on IO, by unsuccessfully attempting to acquire an object's lock, or by invoking an object's wait() method. It can also enter the waiting state by invoking its (deprecated) suspend() method.

43) What is NullPointerException?

A) NullPointerException is thrown when calling the instance method of a null object, accessing or modifying the field of a null object etc.

44) What is the difference between yielding and sleeping?

A) When a task invokes its yield() method, it returns to the ready state. When a task invokes its sleep() method, it returns to the waiting state.


45) There are two classes named classA and classB. Both classes are in the same package. Can a private member of classA can be accessed by an object of classB?

A) Private members of a class aren’t accessible outside the scope of that class and any other class even in the same package can’t access them.

46) What’s difference between Stack and Queue?

A) Stack and Queue both are used as placeholder for a collection of data. The primary difference between a stack and a queue is that stack is based on Last in First out (LIFO) principle while a queue is based on FIFO (First In First Out) principle.

47) What’s the base class of all exception classes?

A) In Java, Java.Lang.throwable is the super class of all exception classes and all exception classes are derived from this base class.

48) Which API is provided by Java for operations on set of objects?

A) Java provides a Collection API which provides many useful methods which can be applied on a set of objects. Some of the important classes provided by Collection API include ArrayList, HashMap, TreeSet and TreeMap.

49) How can we find the actual size of an object on the heap?

A) In java, there is no way to find out the exact size of an object on the heap.

50) How destructors are defined in Java?

A) In Java, there are no destructors defined in the class as there is no need to do so. Java has its own garbage collection mechanism which does the job automatically by destroying the objects when no longer referenced.

51) Is JDK required on each machine to run a Java program?

A) JDK is development Kit of Java and is required for development only and to run a Java program on a machine, JDK isn’t required. Only JRE is required.

52) Can a dead thread be started again?

A) In Java, a thread which is in dead state can’t be started again. There is no way to restart a dead thread.

53) What is daemon thread?

A) A daemon thread is a low priority thread, which runs intermittently in the back ground doing the garbage collection operation for the java runtime system.

54) What is Downcasting?

A) It is the casting from a general to a more specific type, i.e. casting down the hierarchy.

55) What will happen if you put System.exit(0) on try or catch block? Will finally block execute?

A) By Calling System.exit(0) in try or catch block, we can skip the finally block. System.exit(int) method can throw a SecurityException. If System.exit(0) exits the JVM without throwing that exception then finally block will not execute. But, if System.exit(0) does throw security exception then finally block will be executed.

56) I want to persist data of objects for later use. What’s the best approach to do so?

A) The best way to persist data for future use is to use the concept of serialization.

57) How can we make copy of a java object?

A) We can use the concept of cloning to create copy of an object. Using clone, we create copies with the actual state of an object.
Clone() is a method of Cloneable interface and hence, Cloneable interface needs to be implemented for making object copies.

58) What are the two environment variables that must be set in order to run any Java programs?

A) Java programs can be executed in a machine only once following two environment variables have been properly set:
a)PATH variable
b)CLASSPATH variable

59) What will be the output of Round(3.7) and Ceil(3.7)?

A) Round(3.7) returns 4 and  Ceil(3.7) returns 4.

60)Is it possible to define a method in Java class but provide it’s implementation in the code of another language like C?



A) Yes, we can do this by use of native methods. In case of native method based development, we define public static methods in our Java class without its implementation and then implementation is done in another language like C separately.