Java MCQs 1
Java MCQs 1
class Base{
public static void main(String[] args){
System.out.println("Hello");
}
}
ANS : 4
Correct answer/s : 4
This will compile and print "Hello"
The entry point for a standalone java program is
the main method of the class that is being run.
The java runtime system will look for that method
in class Test and find that it does have such a method.
It does not matter whether it is defined in the class itself
or is inherited from a parent class.
QUESTION2
What is the result of trying to compile and run the following code.
public final static void main(String[] args){
double d = 10.0 / -0;
if(d == Double.POSITIVE_INFINITY)
System.out.println("Positive infinity");
else
System.out.println("Negative infinity");
}
ANS : 1
Corr answer : 1
There is no such thing as a positive or negative zero.
Hence the result is always positive infinity.
QUESTION3
1. 2
2. 0
3. 3
4. 2.5
5. 25
ANS : 2
Corr Answer : 2
The result of 1/4 will be zero because integer
divion is carried out on the operands.
If you need to obtain a fractional value
you need to use either a float or double literal
as in 1F / 4F.
QUESTION4
1. int i = 0XCAFE;
2. boolean b = 0;
3. char c = 'A';
4. byte b = 128;
5. char c = "A";
ANS : 1,3
QUESTION5
ANS : 2
Correct answer/s : 2
You are passing a reference to an array as
the argument to the method. The method may not
modify the passed object reference but it can modify
the object itself.
QUESTION6
1. Compiler error.
2. Will throw a NoSuchMethod error at runtime.
3. It will compile and run printing out "10"
4. It will run with no output.
5. It will run and print "10" and then crash with an error.
ANS : 3
Correct answer : 3
This will run, print a message and terminate gracefully.
The runtime system needs to load the class before it can look
for the main method. So the static initializer will run first
and print "10". Immediately after that System.exit(0) will be called
terminating the program before an error can be thrown.
QUESTION7
Is this legal?
long longArr[];
int intArr[] = { 7 ,8 , 9};
longArr = intArr;
1. Yes
2. No
ANS : 2
Correct answer : 2
You cannot assign a reference to an array of primitives
to another unless they contain the same primitive types.
QUESTION8
True or False.
The range of a byte is from -127 to 128
1. True
2. False
ANS : 2
Correct answer/s : 2
The statement is false. The range of an array
is from - 128 to 127
QUESTION9
1. float f = \u0038;
2. long L2 = 2L;
3. float f = 1.2;
4. char c = '/u004E';
5. byte b = 100;
ANS : 1,2,4,5
Corr answer : 1, 2, 5.
1 is correct because \u0038 is unicode for nbr 8.
3 is wrong because 1.2 is a double literal.
4. is a little sneaky perhaps. The
unicode escape character is incorrect
QUESTION10
What is the result of trying to compile and run the following code.
public static void main(String[] args){
double d = 10 / 0;
if(d == Double.POSITIVE_INFINITY)
System.out.println("Positive infinity");
else
System.out.println("Negative infinity");
}
ANS : 4
Corr answer : 4
Division by zero on integer literals will throw
a runtime error.
Q. 1
Which colour is used to indicate instance methods in the standard “javadoc” format
documentation:
A. blue
B. red
C. purple
D. orange
Q. 2
What is the correct ordering for the import, class and package declarations when found in a
single file?
Q. 3
A. equals(String)
B. equals(Object)
C. trim()
D. round()
E. toString()
Q. 4
What is the parameter specification for the public static void main method?
A. String args []
B. String [] args
C. Strings args []
D. String args
Q. 5
What does the zeroth element of the string array passed to the public static void main
method contain?
Q. 6
A. goto
B. malloc
C. extends
D. FALSE
Q. 7
int age;
age = age + 1;
Q. 8
Which of these is the correct format to use to create the literal char value a?
A. �a�
B. “a”
C. new Character(a)
D. \000a
Q. 9
A. 0 - 65, 535
B. (�128) � 127
C. (�32,768) � 32,767
D. (�256) � 255
Q. 10
A. int i = 32;
B. float f = 45.0;
C. double d = 45.0;
Q. 11
age = age + 1;
Q. 12
A. “john” == “john”
B. “john”.equals(”john”)
C. “john” = “john”
D. “john”.equals(new Button(”john”))
Q. 14
B. “john” + 3
C. 3 + 5
D. 5 + 5.5
Q. 15
A. &
B. ||
C. &&
D. |
Q. 16
Q. 17
new Test();
public Test () {
System.out.println(”In test”);
System.out.println(this);
if (temp > 5) {
System.out.println(temp);
Q 18
Q. 19
Q. 20
Q. 21
�
}
Which of the following can be used to define a constructor for this class:
Q. 22
A. if (2 == 3) System.out.println(”Hi”);
B. if (2 = 3) System.out.println(”Hi”);
C. if (true) System.out.println(”Hi”);
D. if (2 != 3) System.out.println(”Hi”);
E. if (aString.equals(”hello”)) System.out.println(”Hi”);
Q. 23
Assuming a method contains code which may raise an Exception (but not a
RuntimeException), what is the correct way for a method to indicate that it expects the
caller to handle that exception:
A. throw Exception
B. throws Exception
C. new Exception
Q. 24
What is the result of executing the following code, using the parameters 4 and 0:
public void divide(int a, int b) {
try {
int c = a / b;
} catch (Exception e) {
System.out.print(”Exception “);
} finally {
System.out.println(”Finally”);
D. No output
Q.25
Which of the following is a legal return type of a method overloading the following method:
A. void
B. int
C. Can be anything
Q.26
Which of the following statements is correct for a method which is overriding the following
method:
Q. 27
class Vehicle {
System.out.println(”Vehicle: drive”);
System.out.println(”Car: drive”);
Vehicle v;
Car c;
v = new Vehicle();
c = new Car();
v.drive();
c.drive();
v = c;
v.drive();
}
What will be the effect of compiling and running this class Test?
C. Prints out:
Vehicle: drive
Car: drive
Car: drive
D. Prints out:
Vehicle: drive
Car: drive
Vehicle: drive
Q. 28
Where in a constructor, can you place a call to a constructor defined in the super class?
A. Anywhere
Q. 29
Which variables can an inner class access from the class which encapsulates it?
Q. 30
QUESTION : 1
1. True.
2. False.
ANS : 2
QUESTION : 2
True or false ?
1. True
2. False
ANS : 2
QUESTION : 3
ANS : 3
QUESTION : 4
ANS : 2,3,4
QUESTION : 5
1. True
2. False
ANS : 1
QUESTION : 6
1. True
2. False
ANS : 2
QUESTION : 7
ANS : 1,2,5,6
QUESTION : 8
Consider the following class
public class Test implements Runnable{
public void run(){}
}
True or False ?
Creating an instance of this class and calling its run() method
will spawn a new thread.
1. True
2. False
ANS : 2
QUESTION : 9
True or false?
A Thread object has a method called notify().
1. False
2. True
ANS : 2
QUESTION : 10
Calling the destroy() method of a thread object relases all the locks held
by
the thread ?
1. True
2. False
ANS : 2
SCJP Collections
Tuesday, September 9th, 2008
QUESTION : 1
What is the result of attempting to compile and run the following code?
ANS : 3
correct answer/s : 3
QUESTION : 2
What is the result of attempting to compile and run the following code?
public class Test {
ANS : 1
correct answer/s : 1
QUESTION : 3
What is the result of attempting to compile and run the following code?
public class Test {
ANS : 4
correct answer/s : 4
QUESTION : 4
1. 8 , 4 and 4
2. 4 , 4 and 8
3. 8 and 4
4. 4 and 8
ANS : 1
correct answer/s : 1
QUESTION : 5
ANS : 1,4
QUESTION : 6
ANS : 3,5
QUESTION : 7
ANS : 1,2,3
QUESTION : 8
True or False.
A WeakHashMap is synchronized.
1. True
2. False
ANS : 2
correct answer/s : 2
QUESTION : 9
True or False.
A Set rejects duplicates and is ordered
1. True
2. False
ANS : 2
correct answer/s : 2
QUESTION : 10
SCJP AWT
Tuesday, September 9th, 2008
QUESTION : 1
What will be the result of compiling and running the following code?
import java.awt.*;
import java.applet.*;
public class AppletTest extends Applet {
Label lbl = new Label("hello");
public void init()
{
setSize(200,100);
setVisible(true);
lbl.setBackground(new Color(0,100,180));
setLayout(new GridLayout(1,1));
add(lbl);
setLayout(new FlowLayout());
lbl.setBounds(0,0,100,24);
}
}
1. The label will fill half the display area of the applet.
2. The label will be wide enough to display the text "hello"
3. The label will not be visiblle.
4. The label will fill the entire display area of the applet
5. The code will throw a run time error because of the second setLayout()
call.
ANS : 2
QUESTION : 2
What will be the result of compiling and running the following applet?
import java.awt.*;
import java.applet.*;
public class AppletTest extends Applet {
public void init()
{
super.init();
PanelTest p = new PanelTest();
p.init();
setVisible(true);
setSize(200,100);
add(p);
}
1. The button will fill the entire display area of the applet.
2. The code will fail to compile.
3. The button will be just big enough to encompass it's label.
4. The applet's display area will be blank.
ANS : 2
QUESTION : 3
This code compiles without error.
class MyButton extends Button implements MouseListener{
public MyButton(String lbl) {
super(lbl);
addMouseListener(this);
}
public void mousePressed(MouseEvent e){
//do something
}
}
1. False
2. True
ANS : 1
QUESTION : 4
1. TextField();
2. TextField(int rows , int cols);
3. TextField(int cols , String txt);
4. TextField(int cols);
5. TextField(String txt , boolean scrollBars);
ANS : 1,4
QUESTION : 5
True or false?
void setResizable(boolean) is a member of the Applet class.
1. True
2. False
ANS : 2
QUESTION : 6
ANS : 1
QUESTION : 7
1. FlowLayout
2. GridLayout
3. CardLayout
4. BorderLayout
5. GridBagLayout
ANS : 4
QUESTION : 8
True or false.
A Dialog is a subclass of Frame.
1. True
2. False
ANS : 2
QUESTION : 9
1. MenuItem()
2. MenuItem(String name)
3. MenuItem(String name , boolean removable)
4. MenuItem(String name , MenuShortcut sc)
5. MenuItem(boolean check)
ANS : 1,2,4
QUESTION : 10
ANS : 3
class Mammal{
void eat(Mammal m){
System.out.println("Mammal eats food");
}
}
class Cattle extends Mammal{
void eat(Cattle c){
System.out.println("Cattle eats hay");
}
}
class Horse extends Cattle{
void eat(Horse h){
System.out.println("Horse eats hay");
}
}
public class Test{
public static void main(String[] args){
Mammal h = new Horse();
Cattle c = new Horse();
c.eat(h);
}
}
ANS : 1
QUESTION : 2
ANS : 1
1. Compiler error.
2. NoSuchMethodException at runtime.
3. Compiles and runs printing 1
4. Throws a NullPointerException at runtime.
ANS : 4
You will get a NullPointerException because the
inner class object gets assigned to the reference a
only after the aMethod() runs. You can prevent
the exception by calling t.aMethod() before the
inner anonymous class method is called.
QUESTION : 4
What will happen if you try to compile and run this code.
class Rectangle{
public int area(int length , int width) {
return length * width;
}
}
ANS : 1
QUESTION : 5
class Base{}
class Derived extends Base{}
public class Test {
public static void main(String[] args){
Derived d = (Derived) new Base();
}
}
ANS : 3
QUESTION : 6
ANS : 4
QUESTION : 7
class Base{
static int value = 0;
Base(){
addValue();
}
static void addValue(){
value += 10;
}
int getValue(){
return value;
}
}
class Derived extends Base{
Derived(){
addValue();
}
static void addValue(){
value += 20;
}
}
public class Test {
public static void main(String[] args){
Base b = new Derived();
System.out.println(b.getValue());
}
}
1. 10
2. 20
3. 30
4. 40
ANS : 3
QUESTION : 8
ANS : 1
QUESTION : 9
class Base{
String s = "Base";
String show(){
return s;
}
}
class Derived extends Base{
String s = "Derived";
}
public class Test {
void print(Base b){
System.out.println(b.show());
}
void print(Derived d){
System.out.println(d.show());
}
public static void main(String[] args){
Test t = new Test();
Base b = new Derived();
t.print(b);
}
}
ANS : 4
QUESTION : 10
What is the result of attempting to compile and run this ?
interface ITest{
public void setVal();
}
public class Test {
private String a;
void aMethod(){
final String b = " World";
ITest it = new ITest() {
public void setVal(){
a = "Hello" + b;
}};
it.setVal();
System.out.println(a);
}
public static void main(String[] args) {
Test t = new Test();
t.aMethod();
}
}
ANS : 3
class Base{
public static void main(String[] args){
System.out.println("Hello");
}
}
ANS : 4
QUESTION2
What is the result of trying to compile and run the following code.
public final static void main(String[] args){
double d = 10.0 / -0;
if(d == Double.POSITIVE_INFINITY)
System.out.println("Positive infinity");
else
System.out.println("Negative infinity");
}
ANS : 1
QUESTION3
1. 2
2. 0
3. 3
4. 2.5
5. 25
ANS : 2
The result of 1/4 will be zero because integer
divion is carried out on the operands.
If you need to obtain a fractional value
you need to use either a float or double literal
as in 1F / 4F.
QUESTION4
ANS : 1,3
QUESTION5
1. Compiler error.
2. Compiles and runs printing out 2
3. Compiles and runs printing out 1
4. An ArrayIndexOutOfBounds Exception at runtime
ANS : 2
QUESTION6
1. Compiler error.
2. Will throw a NoSuchMethod error at runtime.
3. It will compile and run printing out "10"
4. It will run with no output.
5. It will run and print "10" and then crash with an error.
ANS : 3
QUESTION7
Is this legal?
long longArr[];
int intArr[] = { 7 ,8 , 9};
longArr = intArr;
1. Yes
2. No
ANS : 2
QUESTION8
True or False.
The range of a byte is from -127 to 128
1. True
2. False
ANS : 2
Correct answer/s : 2
The statement is false. The range of an array
is from - 128 to 127
QUESTION9
1. float f = \u0038;
2. long L2 = 2L;
3. float f = 1.2;
4. char c = '/u004E';
5. byte b = 100;
ANS : 1,2,4,5
What is the result of trying to compile and run the following code.
public static void main(String[] args){
double d = 10 / 0;
if(d == Double.POSITIVE_INFINITY)
System.out.println("Positive infinity");
else
System.out.println("Negative infinity");
}
ANS : 4
SCJP java.lang
Tuesday, September 9th, 2008
QUESTION : 1
ANS : 4
QUESTION : 2
ANS : 5
1. Is wrong because it is legal to assign integer
literal to a char variable as long as the value
does not exceed the range of a char.
2. Is wrong because parseByte(String s , int radix)
will accept any native numeric type that is not
wider than an int.
3 and 6 are just nonsense.
4. Is wrong because the the character b falls within
the radix range specified by the second parameter.
QUESTION : 3
1. 22
2. 22.0
3. -22
4. -23
5. -22.0
6. 23.0
ANS : 5
QUESTION : 4
ANS : 4
QUESTION : 5
ANS : 2,4,5
QUESTION : 6
ANS : 3
QUESTION : 7
ANS : 1
correct answer/s : 1
QUESTION : 8
1. Compiler error
2. Runtime error
3. Runs and prints "false"
4. Runs and prints "true"
ANS : 2
QUESTION : 9
What is the result of attempting to compile and run this ?
1. Compiler error
2. Runtime error
3. Runs and prints "false"
4. Runs and prints "true"
ANS : 1
QUESTION : 10
1. Compiler error
2. Runtime error
3. Runs and prints "256"
4. Runs and prints "0"
5. Runs and prints "127"
ANS : 4