Java Interview Que
Java Interview Que
Java Interview Que
Questions 1.The Java interpreter is used for the execution of the source code.
a)True
b) False
Answers : a.
Questions 2) On successful compilation a file with the class extension is created.
a)True
b) False
Answers : a.
Questions 3) The Java source code can be created in a Notepad editor.
a)True
b) False
Answers : a.
Questions 4) The Java Program is enclosed in a class definition.
a)True
b) False
Answers : a.
Questions 5) What declarations are required for every Java application?
Answers : A class and the main( ) method declarations.
Questions 6) What are the two parts in executing a Java program and their purposes?
Answers : Two parts in executing a Java program are:
Java Compiler and Java Interpreter.
The Java Compiler is used for compilation and the Java Interpreter is used for execution of
the application.
Questions7) What are the three OOPs principles and define them?
Answers : Encapsulation, Inheritance and Polymorphism are the three OOPs
Principles.
Encapsulation:
Is the Mechanism that binds together code and the data it manipulates, and keeps both safe
from outside interference and misuse.
Inheritance:
Is the process by which one object acquires the properties of another object.
Polymorphism:
Is a feature that allows one interface to be used for a general class of actions.
Questions 8) What is a compilation unit?
Answers : Java source code file.
Questions9) What output is displayed as the result of executing the following statement?
System.out.println("// Looks like a comment.");
// Looks like a comment
The statement results in a compilation error
Looks like a comment
No output is displayed
Answers : a.
Questions 10) In order for a source code file, containing the public class Test, to
successfully compile, which of the following must be true?
It must have a package statement
It must be named Test.java
It must import java.lang
It must declare a public class named Test
Answers : b
Questions 11) What are identifiers and what is naming convention?
Answers : Identifiers are used for class names, method names and variable names. An
identifier may be any descriptive sequence of upper case & lower case letters,numbers or
underscore or dollar sign and must not begin with numbers.
char
Questions 20) What are default values of different primitive types?
Answers : int - 0
short - 0
byte - 0
long - 0 l
float - 0.0 f
double - 0.0 d
boolean - false
char - null
Questions21) Converting of primitive types to objects can be explicitly.
a)True
b)False
Answers : b.
Questions 22) How do we change the values of the elements of the array?
Answers : The array subscript expression can be used to change the values of the elements
of the array.
Questions 23) What is final varaible?
Answers : If a variable is declared as final variable, then you cannot change its value. It
becomes constant.
Questions 24) What is static variable?
Answers : Static variables are shared by all instances of a class.
}
What is output of the above program?
Answers : The value assigned is 20
Questions3) The switch statement does not require a break.
a)True
b)False
Answers : b.
Questions4) The conditional operator is otherwise known as the ternary operator.
a)True
b)False
Answers : a.
Questions5) The while loop repeats a set of code while the condition is false.
a)True
b)False
Answers : b.
Questions6) The do-while loop repeats a set of code atleast once before the condition is
tested.
a)True
b)False
Answers : a.
Questions 7) What are difference between break and continue?
Answers : The break keyword halts the execution of the current loop and forces control out
of the loop.
The continue is similar to break, except that instead of halting the execution of the loop, it
starts the next iteration.
Questions8) The for loop repeats a set of statements a certain number of times until a
condition is matched.
a)True
b)False
Answers : a.
Questions9) Can a for statement loop indefintely?
Answers : Yes.
Questions10) What is the difference between while statement and a do statement/
Answers : A while statement checks at the beginning of a loop to see whether the next
loop iteration should occur.
A do statement checks at the end of a loop to see whether the next iteration of a loop
should occur. The do statement will always execute the body of a loop at least once.
Questions
another.
a)True
b)False
Answers :
Questions
a)True
b)False
Answers :
Questions
a)True
16) Casting between primitive types allows conversion of one primitive type to
a.
17) Casting occurs commonly between numeric types.
a.
18) Boolean values can be cast into any other primitive type.
b)False
Answers : b.
Questions 19) Casting does not affect the original object or value.
a)True
b)False
Answers : a.
Questions 20) Which cast must be used to convert a larger value into a smaller one?
Answers : Explicit cast.
Questions 21) Which cast must be used to cast an object to another class?
Answers : Specific cast.
Questions 22) Which of the following features are common to both Java & C++?
A.The class declaration
b.The access modifiers
c.The encapsulation of data & methods with in objects
d.The use of pointers
Answers : a,b,c.
Questions 23) Which of the following statements accurately describe the use of access
modifiers within a class definition?
a.They can be applied to both data & methods
b.They must precede a class's data variables or methods
c.They can follow a class's data variables or methods
d.They can appear in any order
e.They must be applied to data variables first and then to methods
Answers : a,b,d.
Questions 24) Suppose a given instance variable has been declared private.
Can this instance variable be manipulated by methods out side its class?
a.yes
b.no
Answers : b.
Questions 25) Which of the following statements can be used to describe a public method?
a.It is accessible to all other classes in the hierarchy
b.It is accessablde only to subclasses of its parent class
c.It represents the public interface of its class
d.The only way to gain access to this method is by calling one of the public class
methods
Answers : a,c.
Questions 26) Which of the following types of class members can be part of the internal
part of a class?
a.Public instance variables
b.Private instance variables
c.Public methods
d.Private methods
Answers : b,d.
Questions 27) You would use the ____ operator to create a single instance of a named
class.
a.new
b.dot
Answers : a.
Questions 28) Which of the following statements correctly describes the relation between
an object and the instance variable it stores?
a.Each new object has its own distinctive set of instance variables
b.Each object has a copy of the instance variables of its class
c.the instance variable of each object are seperate from the variables of other objects
d.The instance variables of each object are stored together with the variables of other
objects
Answers : a,b,c.
Questions 29) If no input parameters are specified in a method declaration then the
declaration will include __.
a.an empty set of parantheses
b.the term void
Answers : a.
Questions 30) What are the functions of the dot(.) operator?
a.It enables you to access instance variables of any objects within a class
b.It enables you to store values in instance variables of an object
c.It is used to call object methods
d.It is to create a new object
Answers : a,b,c.
Questions 31) Which of the following can be referenced by this variable?
a.The instance variables of a class only
b.The methods of a class only
c.The instance variables and methods of a class
Answers : c.
Questions 32) The this reference is used in conjunction with ___methods.
a.static
b.non-static
Answers : b.
Questions 33) Which of the following operators are used in conjunction with the this and
super references?
a.The new operator
b.The instanceof operator
c.The dot operator
Answers : c.
Questions 34) A constructor is automatically called when an object is instantiated
a. true
b. false
Answers : a.
Questions 35) When may a constructor be called without specifying arguments?
a. When the default constructor is not called
b. When the name of the constructor differs from that of the class
c. When there are no constructors for the class
Answers : c.
Questions 36) Each class in java can have a finalizer method
a. true
b.false
Answers : a.
Questions 37) When an object is referenced, does this mean that it has been identified by
the finalizer method for garbage collection?
a.yes
b.no
Answers : b.
Questions 38) Because finalize () belongs to the java.lang.Object class, it is present in all
___.
a.objects
b.classes
c.methods
Answers : b.
b.encoding
Answers : b,a.
Questions 50) Which are keywords in Java?
a) NULL
b) sizeof
c) friend
d) extends
e) synchronized
Answers : d and e
Questions 51) When must the main class and the file name coincide?
Answers :When class is declared public.
Questions 52) What are different modifiers?
Answers : public, private, protected, default, static, trancient, volatile, final, abstract.
Questions 53) What are access modifiers?
Answers : public, private, protected, default.
Questions 54) What is meant by "Passing by value" and " Passing by reference"?
Answers : objects pass by referrence
Methods - pass by value
Questions 55) Is a class a subclass of itself?
Answers : A class is a subclass itself.
Questions 56) What modifiers may be used with top-level class?
Answers : public, abstract, final.
Questions 57) What is an example of polymorphism?
Inner class
Anonymous classes
Method overloading
Method overriding
Answers : c
Questions 7) What will happen to the Exception object after exception handling?
8) What is the mechanisam defind by java for the Resources to be used by only one Thread
at a time?
Answers : Synchronisation
9) What is the procedure to own the moniter by many threads?
Answers : not possible
10) What is the unit for 1000 in the below statement?
ob.sleep(1000)
Answers : long milliseconds
11) What is the data type for the parameter of the sleep() method?
Answers : long
12) What are all the values for the following level?
max-priority
min-priority
normal-priority
Answers : 10,1,5
13) What is the method available for setting the priority?
Answers : setPriority()
14) What is the default thread at the time of starting the program?
Answers : main thread
15) The word synchronized can be used with only a method.
True/ False
Answers : False
16) Which priority Thread can prompt the lower primary Thread?
Answers : Higher Priority
17) How many threads at a time can access a monitor?
Answers : one
18) What are all the four states associated in the thread?
Answers : 1. new 2. runnable 3. blocked 4. dead
19) The suspend()method is used to teriminate a thread?
True /False
Answers : False
20) The run() method should necessary exists in clases created as subclass of thread?
True /False
Answers : True
21) When two threads are waiting on each other and can't proceed the programe is said to
be in a deadlock?
True/False
Answers : True
22) Which method waits for the thread to die ?
Answers : join() method
23) Which of the following is true?
1) wait(),notify(),notifyall() are defined as final & can be called only from with in a
synchronized method
2) Among wait(),notify(),notifyall() the wait() method only throws IOException
3) wait(),notify(),notifyall() & sleep() are methods of object class
1
2
3
1&2
1,2 & 3
Answers : D
24) Garbage collector thread belongs to which priority?
Answers : low-priority
25) What is meant by timeslicing or time sharing?
Answers : Timeslicing is the method of allocating CPU time to individual threads in a
priority schedule.
26) What is meant by daemon thread? In java runtime, what is it's role?
Answers : Daemon thread is a low priority thread which runs intermittently in the
background doing the garbage collection operation for the java runtime system.
Answers : False
Questions 17) Declaration of methods as final results in faster execution of the program?
True/False
Answers : True
Questions 18) Final variables should be declared in the beginning?
True/False
Answers : True
Questions 19) Can we declare variable inside a method as final variables? Why?
Answers : Cannot because, local variable cannot be declared as final variables.
Questions20) Can an abstract class may be final?
Answers : An abstract class may not be declared as final.
Questions 21) Does a class inherit the constructors of it's super class?
Answers : A class does not inherit constructors from any of it's super classes.
Questions 22) What restrictions are placed on method overloading?
Answers : Two methods may not have the same name and argument list but different
return types.
Questions 23) What restrictions are placed on method overriding?
Answers : Overridden methods must have the same name , argument list , and return
type. The overriding method may not limit the access of the method it overridees.The
overriding method may not throw any exceptions that may not be thrown by the overridden
method.
Questions 24) What modifiers may be used with an inner class that is a member of an
outer class?
Answers : a (non-local) inner class may be declared as public, protected, private, static,
final or abstract.
Questions 25) How this() is used with constructors?
Answers : this() is used to invoke a constructor of the same class
Questions 26) How super() used with constructors?
Answers : super() is used to invoke a super class constructor
Questions 27) Which of the following statements correctly describes an interface?
a)It's a concrete class
b)It's a superclass
c)It's a type of abstract class
Answers : c
28) An interface contains __ methods
a)Non-abstract
b)Implemented
c)unimplemented
Answers :c
STRING HANDLING
Which package does define String and StringBuffer classes?
Answers : java.lang package.
Which method can be used to obtain the length of the String?
Answers : length( ) method.
How do you concatenate Strings?
Answers : By using " + " operator.
Which method can be used to compare two strings for equality?
Answers : equals( ) method.
Which method can be used to perform a comparison between strings that
}
abcdefabcdef
abcabcDEFDEF
abcdefabcDEF
None of the above
Answers : c.
Which of the following methods are methods of the String class?
delete( )
append( )
reverse( )
replace( )
Answers : d.
Which of the following methods cause the String object referenced by s to
be changed?
s.concat( )
s.toUpperCase( )
s.replace( )
s.valueOf( )
Answers : a and b.
String is a wrapper class?
True
False
Answers : b.
17) If you run the code below, what gets printed out?
String s=new String("Bicycle");
int iBegin=1;
char iEnd=3;
System.out.println(s.substring(iBegin,iEnd));
Bic
ic
c) icy
d) error: no method matching substring(int,char)
Answers : b.
18) Given the following declarations
String s1=new String("Hello")
String s2=new String("there");
String s3=new String();
Which of the following are legal operations?
s3=s1 + s2;
s3=s1 - s2;
c) s3=s1 & s2
d) s3=s1 && s2
Answers : a.
19) Which of the following statements are true?
The String class is implemented as a char array, elements are addressed
using the stringname[] convention
b) Strings are a primitive type in Java that overloads the + operator for
concatenation
c) Strings are a primitive type in Java and the StringBuffer is used as the
matching wrapper type
d) The size of a string can be retrieved using the length property.
Answers : b.
EXPLORING JAVA.LANG
java.lang package is automatically imported into all programs.
True
False
Answers : a
What are the interfaces defined by java.lang?
Answers : Cloneable, Comparable and Runnable.
What are the constants defined by both Flaot and Double classes?
Answers : MAX_VALUE,
MIN_VALUE,
NaN,
POSITIVE_INFINITY,
NEGATIVE_INFINITY and
TYPE.
What are the constants defined by Byte, Short, Integer and Long?
Answers : MAX_VALUE,
MIN_VALUE and
TYPE.
What are the constants defined by both Float and Double classes?
Answers : MAX_RADIX,
MIN_RADIX,
MAX_VALUE,
MIN_VALUE and
TYPE.
What is the purpose of the Runtime class?