5

Java Interview Questions

[Social9_Share type="horizontal"]

Java Interview Questions – 50

1.What do you know about Java?
Java is a high-level programming language. It is platform independent. The java code runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. Add answers of Q:3 also

2.What are the supported platforms by Java Programming Language?
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.List any five features of Java?
Some features include Object Oriented, Platform Independent, Robust, Interpreted, Multi-threaded,Dynamic,Distributed

4.Why is Java Architectural Neutral?
Javac compiler generates an architecture-neutral object file format which is also called as byte code and makes the compiled code to be executable on many processors using JVM.

5.What is JVM ?
When Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web and interpreted by Java Virtual Machine (JVM) on whichever platform it is being run.

6.Is JVM platform independent?
No.

7.List some Java keywords ?
Some Java keywords are import, super, finally, abstract etc.

8.List the primitive data types of java?
byte, char, int, long, float, double and boolean

9.What do you mean by object?
Anything can be a object. A car, a pen, a laptop. An object its own attributes and behavior
The attributes are like variables.
(eg)
A pen has colour, size, shape
A car has model, price, mileage.
The behaviors are like methods what do you with car object
(ie) getFeatures(), getPriceDetails() etc.

10.Define a class?
A class is a collection of objects. A class is a blueprint from which objects are created.
(eg)
collection of car objects- Car class

11.What is a local variable?
Variables within methods, blocks and constructor are called local variable. Their scope is within the method only. They don’t take any default value. They must be initialized before using them. If it is not initialized , will give compiler error.

12.What is a instance variable ?
These are variables for an object. Each object has its own set of properties which are called as variables(values are different). The values are initialized to default values on creation of the object

13.What is a static variable ?
These variables are available as a single copy in a class. All the objects in a class share the same copy. Only static variables are initialized to their default values when the class gets loaded.

14.What is a constructor?

  • Is used to initialize the instance variables
  • Comes with class name & has no return type
  • Can be private also
  • Gets invoked automatically when an object is created
  • The compiler creates a default constructor (without parameters), if you do not create any constructor
  • A class can have more than one constructor(parameterized)
  • You can never call a constructor by name

15.How will you create an Object?
Use new operator to create an object.

16.What is the default value of boolean?
false

17.What is the default floating point type in Java?

– double. It takes 64 bits.
– If you want declare float use float a= 10.5f;

18.What is the use of char?
‘char’ is a data type which is a unicode representation of characters. It is also used for special characterslike backspace, newline, tab etc

19.What are the different access specifiers in java?
public, protected, default, private

20.What is the access specifier used for class?
public & default only. An inner class can be private, as it acts like a class member.


21.What is the access specifier used for method?
public, protected, default & private

22.What is the access specifier used for local variable?
final

23.What happens if you don’t inilialize the local variables?
It gives a compile error.

24.What type of parameter does switch accept?
switch accepts variable that can be of type byte, short, and int. switch in java 7 accepts String also.

25.Is it mandatory to have default case in ‘switch’ statement?
No. It is optional.

26.Which is the top level of class in java?
Object is the class which is extended by all other class.

27.Which package is imported by default in java?
java.lang is imported by default

28.Can a java file have more than one java class?
Yes, you can have any number of java classes in a single file. You can save the file in any name. In case of multiple classes, only one class can be public. In this case you must save the file in public class name.

29.Can I have more than one main method in a class?
Yes, but with different parameters. But JVM will check for the method with public static void main(String a[]) as parameter. If not available will throw an exception as Main method not found

30.What is String?
String is a final class. String is fixed and immutable.

31.Why String is immutable?
Once a string object is created, it cannot be changed. So it can be safely shared across threads.
(eg)
String s =”hello”;
s.toUpperCase();
System.out.println(s); // will print ”hello” only.
“HELLO” is created and it will be lost as it is not referenced by any variable..

32.What is inheritance?
Inheritance is used for reusability. The sub class can use the same variables and methods of the super class and also can have its own method.

33.What type of inheritance does java supports?
Simple and Multilevel.


34.Why multiple inheritance is not supported in java?
If two class have a method with the same name, when the child class extending these classes try to invoke the method, there is a confusion, for the child class ,that from which parent class to invoke the method. So multiple inheritance is not supported in java.

35.What is compile time polymorphism?
It is also called as overloading. Java supports constructor overloading and method overloading. Since the method/constructors are paired during the compile time itself after checking for the parameter, it is called as compile time polymorphism.
If the matching method/ constructor is not available, it will give a compiler error.

36.What is the method overloading & purpose?
Method overloading is, having the method being overloaded with one to many parameters. Here

  • the method name must be same
  • return type can be same or different
  • the number of parameters or the parameter data type should be different

Method Overloading is used to get different functionality from the overloaded methods having same name.

37.What is constructor overloading?
Having the more than one constructor with one to many parameters is called as constructor overloading

38.Why constructor overloading is called as complier time polymorphism?
While creating an object, the complier checks for a constructor with matching parameters, if it is not available, it will give a compiler error.

39.Can a constructor be private?
Yes.

40.How to invoke one constructor from another constructor?
Using this()keyword with matching parameter.

41.What is the use of ‘this()’ keyword?
this() keyword is used to call one constructor from another by passing matching parameter in the same class

42.What is the use of ‘this’ keyword?
‘this’ refers to the current object. It prevents the local variable hiding the instance variable, when the instance variable name and the local variable name are same

43.What is Var-args?
Var args is variable argument list. It can used within a method as parameter. It can values as zero to many. It similar to an array

  • You can have only one var- args list in a method
  • The var args list must be the last parameter
  • The data type is followed with 3 dots

(eg)void calAvg (String a, int…marks);

44.What is the difference between Overloading and Var args?
Overloading is used when the method name is same, but the method parameters and functionality are different.
eg. calculating area for different shapes

Var-args list is used to when the functionality is same and the method values are different for different method call.
eg. calculating sum of marks which may vary from one subject to many subjects for students

45.What is the use of super() keyword?
super() should be in the first line of a subclass constructor.
It is used to call the constructor of the super class.
The parameter within super must match at least one constructor of super class.

46.What is Runtime Polymorphism?
It is also called as overriding. It happens in a super class, subclass scenario.
The super class decides which subclass to class to call only during the runtime.
This is done using superclass ref = subclass object

47.What is the static?
static is a keyword. It can be as block, variables, method, and as top level of nested class.
The method/variable marked as static is available as a single copy only. All the objects share the same value only.
You can call the static method/variables without creating an object.
They can be called using
classname.method name or classname.variablename.
(eg).
Integer.parseInt(“100″)

48.How will you call a non static(normal) method from a static method?
Use object to call a non static method
(eg)
calling a method from public static void main(String args[]) using object

49.What is the use of static import?
The static import is used to import the static member of one class to another class. The syntax
import static pkg.subpkg.classname.*;
This imports all the static members of a class into the calling class
eg.
import static java.lang.Integer.*;
This imports all the static members of a Integer class into the calling class

50.Can you refer to a super class object(using ‘super’ keyword) or a current object(using ‘this’ keyword) inside a static method?
No. super and this keyword cannot be used inside a static method.
Still More ………………….

By Madheswaran

Happy Learning

Comments(5)

  1. Reply
    Naresh says

    I have been reading your stuff Java Interview Questions and they are too much useful for me to know something new and interesting.

  2. Reply
    Rajitha says

    Your information is really useful for Java beginners and job seekers.

  3. Reply
    mani says

    Very great post. I just stumbled upon your weblog and
    wished to say that I’ve truly enjoyed surfing around your weblog posts.
    In any case I will be subscribing in your rss feed and I hope you write once more soon!

  4. Reply
    bandar says

    Howdy! This blog post could not be written much better! Thank you for sharing!

  5. Reply
    Apurva says

    Your blog is very helpful and informative indeed. Thank you for sharing.

Post a comment