Professional Documents
Culture Documents
com 1
Programs on CoreJava
int i = 32;
float f = 45.0;
double d = 45.0;
/*
float f = 45.0;
*/
2) public class Q2 {
static int age;
public static void main (String args []) {
age = age + 1;
System.out.println("The age is " + age);
}
}
/*
Compiles and runs printing out The age is 1
*/
3) Which of the following are correct?
/*
128 >> 1 gives 64
128 >>> 1 gives 64
*/
4) Which of the following return true?
"john" == "john"
"john".equals("john")
"john" = "john"
"john".equals(new Button("john"))
myclipta.blogspot.com 2
/*
"john" == "john"
"john".equals("john")
*/
5) Which of the following are so called "short circuit" logical operators?
&
||
&&
|
/*
||
&&
*/
/*
Object o = new Button("A");
Panel p = new Applet();
*/
/*
The value 10 is one of the elements printed to the standard output
*/
/*
String temp [] = {"a", "b", "c"};
*/
/*
public abstract void add();
*/
/*
When the class has no other constructors
*/
if (2 == 3) System.out.println("Hi");
if (2 = 3) System.out.println("Hi");
if (true) System.out.println("Hi");
if (2 != 3) System.out.println("Hi");
if (aString.equals("hello")) System.out.println("Hi");
myclipta.blogspot.com 4
/*
if (2 == 3) System.out.println("Hi");
if (true) System.out.println("Hi");
if (2 != 3) System.out.println("Hi");
if (aString.equals("hello")) System.out.println("Hi");
*/
12) Assuming a method contains code which may raise an Exception (but not a Runtime
Exception), what is the correct way for a method to indicate that it expects the caller to
handle that exception:
throw Exception
throws Exception
new Exception
Don't need to specify anything
/*
throws Exception
*/
/*
Prints out: Exception Finally
*/
14) Which of the following statements is correct for a method which is overriding the
following method:
/*
the overriding method must return void
*/
class Vehicle {
public void drive() {
System.out.println("Vehicle: drive");
}
}
What will be the effect of compiling and running this class Test?
Vehicle : drive
Car : drive
Car : drive
Prints out:
myclipta.blogspot.com 6
Vehicle : drive
Car : drive
Vehicle : drive
/*
Prints out:
Vehicle : drive
Car : drive
Car : drive
*/
16) Where in a constructor, can you place a call to a constructor defined in the super
class?
/*
The first statement in the constructor
*/
17) Which variables can an inner class access from the class which encapsulates it?
/*
All static variables
All final variables
All instance variables
*/
/*
Any class or interface
*/
19) In the following code, which is the earliest statement, where the object originally
held in e, may be garbage collected:
Line 7
Line 8
Line 10
Line 11
Never
/*
Line 7
*/
20) What is the name of the interface that can be used to define a class that can execute
within its own thread?
Run
Runnable
Thread
Threadable
Executable
/*
Runnable
*/
21) What is the name of the method used to schedule a thread for execution?
myclipta.blogspot.com 8
init();
start();
run();
resume();
sleep();
/*
start();
*/
sleep();
stop();
yield();
wait();
notify();
/*
sleep();
stop();
yield();
wait();
*/
/*
myclipta.blogspot.com 9
Produces no output
*/
If a notify() method has already been sent to that object then it has no effect
The object issuing the call to wait() will halt until another object sends a notify() or
notifyAll() method
An exception will be raised
The object issuing the call to wait() will be automatically synchronized with any other
objects using the receiving object.
/*
The object issuing the call to wait() will halt until another object sends a notify() or
notifyAll() method
*/
27)What is the effect of adding the sixth element to a vector created in the following
manner:
/*
The vector grows in size to a capacity of 15 elements
*/
28) What is the result of executing the following code when the value of x is 2:
switch (x) {
case 1:
System.out.println(1);
case 2:
case 3:
System.out.println(3);
case 4:
System.out.println(4);
/*
The values 3 and 4 are printed out
*/
/*
true is printed to standard out
*/