Documente Academic
Documente Profesional
Documente Cultură
Which of the following correctly declare a variable which can hold an array of 10
integers?
Question 2 of 61
What will be the result of attempting to compile and run the following code?
public class Nesting
{
public static void main(String args[])
{
B.C obj = new B( ).new C( );
}
}
class A
{
char c;
A(char c) { this.c = c; }
}
class B extends A
{
char c = 'a';
B( ) { super('b'); }
class C extends A
{
char c = 'c';
C( )
{
super('d');
System.out.println(B.this.c);
System.out.println(C.this.c);
System.out.println(super.c);
}
}
}
Question 5 of 61
What will be the result of attempting to compile and run the following class?
public class IfTest
{
public static void main(String args[])
{
if (true)
if (false)
System .out.println("True False");
else
System.out.println("True True");
}
}
Question 7 of 61
Question 9 of 61
Which of the following implementations of a max() method will correctly return the
largest value?
// (1)
int max(int x, int y)
{
return( if(x > y){ x; } else{ y; } );
}
// (2)
int max(int x, int y)
{
return( if(x > y){ return x; } else{ return y; } );
}
// (3)
int max(int x, int y)
{
switch(x < y)
{
case true:
return y;
default :
return x;
};
}
// (4)
int max(int x, int y)
{
if (x > y) return x;
return y;
}
Question 12 of 61
Which of the following are correct ways to initialize the static variables MAX and
CLASS_GUID ?
class Widget
{
static int MAX; //1
static final String CLASS_GUID; // 2
Widget()
{
//3
}
Widget(int k)
{
//4
}
}
Question 13 of 61
Given the following classes and declarations, which of these statements about //1 and //2
are true?
class A
{
private int i = 10;
public void f(){}
public void g(){}
}
class B extends A
{
public int i = 20;
public void g(){}
}
public class C
{
A a = new A();//1
A b = new B();//2
}
Question 14 of 61
You want to invoke the overridden method from the overriding method named m().
Write the construct which will let you do so.
Question 15 of 61
Which of the following lines can be inserted at line 1 to make the program run?
//line 1
public class TestClass
{
public static void main(String[] args)
{
PrintWriter pw = new PrintWriter(System.out);
OutputStreamWriter osw = new OutputStreamWriter( System.out);
pw.print("hello");
}
}
Question 16 of 61
Given the following code, which statements can be placed at the indicated position
without causing compile and run time errors?
public class Test
{
int i1;
static int i2;
public void method1()
{
int i;
// ... insert statements here
}
}
After what line string "string 2" will be eligible for garbage collection?
public class TestClass
{
public static void main(String args[])
{
String s ;
String s1 = new String("string 1");
String s2 = new String("string 2");
String s3 = new String("string 3");
s = s2;
s2 = null; //(1)
s = s1 + s2 +s3; //(2)
s1 = null ; //(3)
s = null; //(4)
s2 = null; //(5)
s3 = null; //(6)
}}
Question 18 of 61
Which one of the following class definitions is/are a legal definition of a class that cannot
be instantiated?
class Automobile
{
abstract void honk(); //(1)
}
abstract class Automobile
{
void honk(); //(2)
}
abstract class Automobile
{
void honk(){}; //(3)
}
abstract class Automobile
{
abstract void honk(){} //(4)
}
abstract class Automobile
{
abstract void honk(); //(5)
}
Given the following classes, what will be the output of compiling and running the class
Truck?
class Automobile
{
public void drive() { System.out.println("Automobile: drive"); }
}
public class Truck extends Automobile
{
public void drive() { System.out.println("Truck: drive"); }
public static void main (String args [ ])
{
Automobile a = new Automobile();
Truck t = new Truck();
a.drive(); //1
t.drive(); //2
a = t; //3
a.drive(); //4
}
}
Question 21 of 61
Which of the following statements will correctly create and initialize an array of Strings
to non null elements?
Question 22 of 61
Question 23 of 61
Question 24 of 61
Question 26 of 61
What will be the result of attempting to compile and run the following program?
Question 27 of 61
Question 28 of 61
Question 29 of 61
Question 30 of 61
Question 31 of 61
Which of the following code fragments show syntactically correct usage of assertions?
a.)
public void assertTest(Object obj)
{
assert obj != null : throw AssertionError();
}
b.)
public void assertTest(Object o1, Object o2)
{
assert o1, o2 != null;
}
c.)
public void assertTest(Vector v)
{
assert v.isEmpty();
}
d.)
public void assertTest()
{
assert;
}
e.)
public void assertTest()
{
assert null;
}
What will be the result of attempting to compile and run the following class?
public class NumberTest
{
public static void main(String args[])
{
System.out.println(0x10 + 10 + 010);
}
}
Question 33 of 61
Question 34 of 61
What is the name of the method that threads can use to pause their execution until
signalled by another thread to continue ?
(Do not include a parameter list or brackets or semicolon.)
Question 36 of 61
An overriding method must have a same parameter list and same return type as that
of the overridden method.
Question 37 of 61
Question 38 of 61
What command should be given to compile and run a java source file named
TestClass.java (for standard JDK)?
How many objects are eligible for garbage collection when the control reaches line 4 if
the method process() is called with the argument of 5?
Question 40 of 61
class Base
{
// NullPointerException is a subclass of RunTimeException.
// IOException is a not subclass of RunTimeException.
void method1() throws java.io.IOException, NullPointerException
{
someMethod("arguments");
// some I/O operations
}
int someMethod(String str)
{
if(str == null) throw new NullPointerException();
else return str.length();
}
}
public class NewBase extends Base
{
void method1()
{
someMethod("args");
}
}
Question 43 of 61
What is the result of executing the following code when the value of i is 5:
switch (i)
{
default:
case 1:
System.out.println(1);
case 0:
System.out.println(0);
case 2:
System.out.println(2);
break;
case 3:
System.out.println(3);
}
Question 44 of 61
Question 45 of 61
Question 46 of 61
Consider the following code that uses an assertion to ensure that the program is supplied
with 2 arguments:
public class TestClass
{
public static void main(String[] args)
{
assert args.length == 2 : "Must give two arguments";
...
}
}
Which of the given statements regarding the above code are correct?
Question 47 of 61
Which is the first line that will cause compilation to fail in the following program?
// Filename: A.java
class A
{
public static void main(String args[])
{
A a = new A();
B b = new B();
a = b; // 1
b = a; // 2
a = (B) b; // 3
b = (B) a; // 4
}
}
class B extends A { }
Question 48 of 61
Question 50 of 61
Write the name of the interface that can be implemented by a class so that it can execute
in a seperate thread?
(Do not put spaces, brackets or any other special charaters)
Question 51 of 61
What should be inserted at //line 1 to make sure that it prints h.i = 20?
class Hello implements Runnable{
int i;
public void run(){
try
{
Thread.sleep(3000);
}
catch (InterruptedException e){}
i = 20;
}
}
public class Test
{
static public void main(String[] args) throws Exception{
Hello h = new Hello();
Thread t = new Thread(h);
t.start();
//line 1
System.out.println("h.i = " + h.i);
}
}
a. h.wait()
b. t.wait()
c. y.yield()
d. t.join()
e. h.notify()
f. t.notify()
g. t.interrupt()
Given the following pairs of method declarations, which of the statements are true?
1.
void perform_work(int time){ }
int perform_work(int time, int speed){ return time*speed ;}
2.
void perform_work(int time){ }
int perform_work(int speed){return speed ;}
3.
void perform_work(int time){ }
void Perform_work(int time){ }
Question 53 of 61
Which of these parameter lists do not have a corresponding constructor in the String
class?
What will be the output when the following class is compiled and run?
class ScopeTest
{
static int x = 5;
public static void main(String[] args)
{
int x = ( x=3 ) * 4; // 1
System.out.println(x);
}
}
Question 55 of 61
Question 57 of 61
Question 58 of 61
class A
{
protected int i;
A(int i) { this.i = i; }
}
Which of the following would be a valid inner class for this class?
Select 2 correct options
a class B {}
b class B extends A {}
c class B { B() { System.out.println("i = " + i); } }
d class B { class A {} }
e class A {}
Question 59 of 61
Question 60 of 61
What happens when a method calls wait() without ensuring that the current thread owns
the monitor of the object?