Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
924 views

Java Test 7 Aug 2k9

This document contains instructions and questions for a Java test. It states that the test contains 40 multiple choice questions worth 1 mark each. Students must score a minimum of 50% to pass. It provides sample code and questions related to Java concepts like threads, exceptions, inheritance and more.

Uploaded by

Nitin Goyal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
924 views

Java Test 7 Aug 2k9

This document contains instructions and questions for a Java test. It states that the test contains 40 multiple choice questions worth 1 mark each. Students must score a minimum of 50% to pass. It provides sample code and questions related to Java concepts like threads, exceptions, inheritance and more.

Uploaded by

Nitin Goyal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Java Test 3

Max Marks: 40 Max Time:1 Hr 30 Min

Convener: Ritu Chawla

Important Instructions

 This Question Paper contains a total of 40 questions. All questions are


mandatory.
 All the questions are objective type and may have more than one option
correct. Each question carries 1 mark. There is no negative marking.
 An answer to the question is considered to be correct only if all correct option(s)
are chosen by student.
 Students need to score minimum 50% marks to pass this exam.

Question 1) What will happen when you attempt to compile and run the following
code?
public class Tux extends Thread{
static String sName = "vandeleur";
public static void main(String argv[]){
Tux t = new Tux();
t.piggy(sName);
System.out.println(sName);
}

public void piggy(String sName){


sName = sName + " wiggy";
start();
}
public void run(){
for(int i=0;i < 4; i++){
sName = sName + " " + i;
}}}}

A) Compile time error


B) Compilation and output of "vandeleur wiggy"
C) Compilation and output of "vandeleur wiggy 0 1 2 3"
D) Compilation and output of either "vandeleur", "vandeleur 0", "vandeleur
01" "vandaleur 0 1 2" or "vandaleur 0 1 2 3"

Question 2) Consider the code below:

class A extends Thread {


private int i;
public void run() {i = 1;}
public static void main(String[] args) {
A a = new A();
a.run();
System.out.print(a.i);
}}

What will be the output of the code above?


A) Prints nothing
B) Prints: 1
C) Prints: 01
D) Compile-time error
E) None of the above

Question 3)

class C1
{
static class C2
{
static int i1;
}
public static void main(String a[])
{
System.out.println(C1.C2.i1);
}
}

What is the result of attempting to compile and run the program?


A) prints 0
B) Compile time error
C) Runtime exception
D) None of the above

Question 4)

class A extends Thread {


private int i;
public void run() {i = 1;}
public static void main(String[] args) {
A a = new A();
a.run();
System.out.print(a.i);
}}

How many threads are created in this Program?

A) 1
B) 2
C) 3
D) 0
E) None of the above

Question 5) What will be the result of attempting to compile and run the
following program?

public class MyClass extends Thread {


public MyClass(String s) { msg = s; }
String msg;
public void run() {
System.out.println(msg);
}

public static void main(String[] args) {


new MyClass("Hello");
new MyClass("World");
}
}

Select the one correct answer.

A) The program will fail to compile.


B) The program will compile without errors and will print Hello and World, in
that order, every time the program is run.
C) The program will compile without errors and will print a never-ending
stream of Hello and World.
D) The program will compile without errors and will print Hello and World
when run, but the order is unpredictable.
E) The program will compile without errors and will simply terminate
without any output when run.

Question 6) Which statements are true?

Select the two correct answers.

a. The class Thread is abstract.


b. The class Thread implements Runnable.
c. Classes implementing the Runnable interface must define a method
named start.
d. Calling the method run() on an object implementing Runnable will create a
new thread.
e. A program terminates when the last non-daemon thread ends.

Question 7) Which methods are required to implement the interface Runnable.

A) wait()
B) run()
C) stop()
D) update()
E) resume()

Question 8) Which class defines the wait() method?

Question 9) For what reasons might a thread stop execution?

A) A thread with higher priority began execution.


B) The thread’s wait() method was invoked.
C) The thread invoked its yield() method.
D) The thread’s pause() method was invoked.
E) The thread’s sleep() method was invoked.

Question 10) Which one of the following is a valid declaration of an applet?

A) Public class MyApplet extends java.applet.Applet {


B) Public Applet MyApplet {
C) Public class MyApplet extends applet implements Runnable {
D) Abstract class MyApplet extends java.applet.Applet {
E) Class MyApplet implements Applet {

Question 11)

import java.awt.*;
class A extends Frame implements ActionListener,
WindowListener {
public A() {
Button button1 = new Button("Click Me");
button.addActionListener(this);
add(button1);
show();
}
public actionPerformed(ActionEvent e) {}
void b1() { return; }
}

Referring to the above, what statement is added to actionPerformed() so that b1()


is invoked for a click on button1?

A) b1();
B) if(e.getActionCommand().equals("button1")) b1();
C) if(e.target == button1) b1();
D) if(e.getSource().equals(button1)) b1();
E) if(e.type == ButtonAction) b1();

Question 12) What will happen when you attempt to compile and run the
following code?

class c1{
void go(){}
}
class c2 extends c1
{
String go()
{
return null;
}
}

A) compile time error


B) runtime exception
C) the code compiles and runs fine
D) None of the above

Question 13)

class command {
public static void main (String[] a1) {
System.out.println(a1.length()); //1
System.out.println(a1[0]); //2
System.out.println(a1); //3
}}

What is the result of attempting to compile and run the program?


A) compile time error at line1
B) compile time error at line2
C) compile time error at line3
D) Runtime exception

Question 14)

abstract class A {} // 1
transient class B {} // 2
private class C {} // 3
static class D {} // 4

Which of these declarations will not produce a compile-time error?

A) 1
B) 2
C) 3
D) 4
E) None of the above

Question 15) Select three correct statements about the following code.

public class TechnoSample {


public static void main(String[] args) {
TechnoSample myref = new TechnoSampleSub();
try{
myref.test();
}
catch(Exception e){}
}
void test() throws Exception{
System.out.println("In TechnoSample");
throw new Exception();
}
}
class TechnoSample Sub extends TechnoSample {
void test() {
System.out.println("In TechnoSampleSub");
}
}

A) The try-catch block that encloses myref.test(); is mandatory for the code
to compile
B) Prints: In TechnoSample
C) Prints: In TechnoSampleSub
D) Method test() in class TechnoSampleSub has no obligation to declare a
throws clause
E) An exception is thrown at runtime

Question 16) What if the static modifier is removed from the signature of the
main method?

Question 17) For what reasons might a thread stop execution?

Question 18) Which of the following expressions will produce errors upon
compilation?

(A) boolean a = (boolean) 1;


(B) boolean b = (false && true);
(C) float y = 22.3;

Choose the correct option.


a. (A) & (C)
b. (A)
c. (A), (C) & (D)
d. (A), (B) & (D)

Question 19) For the given following code, which is true?

class Hmpf
{

//------

class Zing
{
protected Hmpf h;
}

//-------

class Woop extends Zing


{

A. Woop IS-A Hmpf and HAS-A zing.


B. zing IS-A Woop and HAS-A Hmpf.
C. Hmpf HAS-A Woop and Woop IS-A Zing.
D. Woop HAS-A Hmpf and Woop IS-A zing.
E. Zing HAS-A Hmpf and Zing IS-A Woop.

Question 20) Given the following code, which line of code, inserted at indicated
position, will NOT compile?

public class Test


{
public static void main(String ar[])
{
int x;
x = test();
}

static int test()


{
// --position -- insert code here
return y;
}
}

A) short y = 7;
B) int y = (int) 7.2d;
C) Byte y = 7;
D) char y = 's';
E) int y = 0xface;
F) None of the above

Question 21) Given the following code, which of the methods, if inserted
independently at indicated position, will NOT compile?

class Over
{
int doStuff(int a, float b)
{
return 7;
}
}

class Over2 extends Over


{
// --position -- insert code here
}

A) public int doStuff(int x, float y) { return 4; }


B) protected int doStuff(int x, float y) {return 4; }
C) private int doStuff(int x, float y) {return 4; }
D) private int doStuff(int x, double y) { return 4; }
E) long doStuff(int x, float y) { return 4; }
F) int doStuff(float x, int y) { return 4; }

Question 22) Given the default classpath:


/foo
And this directory structure:
foo
|
test
|
xcom
|--A.class
|--B.java
And these two files:
package xcom;
public class A { }
package xcom;
public class B extends A { }

Which allows B.java to compile? (Choose all that apply.)


A. Set the current directory to xcom then invoke
javac B.java
B. Set the current directory to xcom then invoke
javac -classpath . B.java
C. Set the current directory to test then invoke
javac -classpath . xcom/B.java
D. Set the current directory to test then invoke
javac -classpath xcom B.java
E. Set the current directory to test then invoke
javac -classpath xcom:. B.java

Question 23) Consider the code below:

void myMethod()
{ try
{
fragile();
}
catch( NullPointerException npex )
{
System.out.println( "NullPointerException thrown " );
}
catch( Exception ex )
{
System.out.println( "Exception thrown " );
}
finally
{
System.out.println( "Done with exceptions " );
}
System.out.println( "myMethod is done" );
}

What is printed to standard output if fragile()


throws an IllegalArgumentException?

a) "NullPointerException thrown"
b) "Exception thrown"
c) "Done with exceptions"
d) "myMethod is done"
e) Nothing is printed
Question 24) Consider the following code sample:

class Tree{}
class Pine extends Tree{}
class Oak extends Tree{}
public class Forest
{ public static void main( String[] args )
{ Tree tree = new Pine();

if( tree instanceof Pine )


System.out.println( "Pine" );

if( tree instanceof Tree )


System.out.println( "Tree" );

if( tree instanceof Oak )


System.out.println( "Oak" );

else System.out.println( "Oops" );


}
}
Select all choices that will be printed:

a) Pine
b) Tree
c) Forest
d) Oops
e) (nothing printed)

Question 25) If size = 4, triArraylooks like:

int[][] makeArray( int size)


{ int[][] triArray = new int[size] [];
int val=1;
for( int i = 0; i < triArray.length; i++ )
{ triArray[i] = new int[i+1];
for( int j=0; j < triArray[i].length; j++ )
{ triArray[i][j] = val++;
}
}
return triArray;
}

a)
1234
567
89
10

b)
1 4 9 16

c)
1234

d)
1234
5678
9 10 11 12
13 14 15 16

e)
1
23
456
7 8 9 10

Question 26) Consider the two statements:

1. boolean passingScore = false && grade == 70;


2. boolean passingScore = false & grade == 70;

The expression

grade == 70

is evaluated:
a) in both 1 and 2
b) in neither 1 nor 2
c) in 1 but not 2
d) in 2 but not 1
e) invalid because false should be
FALSE

Question 27) In the list below, which subclass(es) of Component cannot be


directly instantiated:
a)Panel
b) Dialog
c) Container
d) Frame

Question 28)

class C{
public static void main(String[] args) {
try
{
int i1=3/0;
}
catch(Exception e)
{
System.out.println("exception1");
}
catch(NullPointerException e)
{
System.out.println("exception2");
}
finally
{
System.out.println("finally");

}
}}

A) compile time error


B) runtime exception
C) prints exception1 and finally
D) prints exception1,exception2 and finally
E) None of the above

Question 29)

class C {
private String get ( String str ) {
try
{
throw new Exception ( ) ;
return str ;
}
catch ( Exception e )
{
return null ;
}
}
public static void main ( String peace [ ] ) {
try
{
System.out.println ( ( new C ( ) ).get ( " C " ) ) ;
}
catch ( Exception e )
{
System.out.println( "Exception" ) ;
}
}
};

A) compile time error


B) prints Exception
C) the code compiles and runs fine
D) none of the above

Question 30)

class C{
public static void main(String[] args) {
try
{
try
{
try
{
}
catch(RuntimeException e)
{
}
}
catch(Exception e)
{
}

}
catch(NullPointerException e)
{
}
finally
{
System.out.println("finally");

}
}}
A) Prints finally
B) compile time error
C) Runtime Exception
D) None of the above

Question 31) Select three correct statements about the following code.

public class TechnoSample {


public static void main(String[] args) {
TechnoSample myref = new TechnoSampleSub();
try{
myref.test();
}
catch(Exception e){}
}
void test() throws Exception{
System.out.println("In TechnoSample");
throw new Exception();
}
}
class TechnoSample Sub extends TechnoSample {
void test() {
System.out.println("In TechnoSampleSub");
}
}

(1) The try-catch block that encloses myref.test(); is mandatory for the code to compile
(2) Prints: In TechnoSample
(3) Prints: In TechnoSampleSub
(4) Method test() in class TechnoSampleSub has no obligation to declare a throws
clause
(5) An exception is thrown at runtime

Question 32) What is the output of the following code ?

FILE 1
package name.mihaiu.test1;
public class CTest1
{
static String name = "CTest1";
}

FILE 2
package name.mihaiu.test2;
public class CTest2 extends name.mihaiu.test1.CTest1
{
public static void main(String args[])
{
System.out.println("Class name =" + name);
}
}

Question 33) What is the output of the following code ?


public class CDummy
{
public static void main(String args[])
{
try {
testX();
}
catch (Exception ee)
{
System.out.println("Exception !");
}
}

public static int testX() throws ArithmeticException


{
try {
throw new ArithmeticException();
}finally {
System.out.println("Finally");
return 4;
}
}
}

A) Exception !
B) Finally
C) Compile time error
D) None of these

Question 34) Which of the following describe the sequence of method calls that
result in a component being redrawn?

a) invoke paint() directly


b) invoke update which calls paint()
c) invoke repaint() which invokes update(), which in turn invokes paint()
d) invoke repaint() which invokes paint directly
Question 35) What method(s) from the java.lang.Math class might method() be if
the statement

method( -4.4 )
== -4;

is true.

a)round()
b) min()
c) trunc()
d) abs()
e) floor()
f) ceil()

Question 36) Which digits, and in which order, will be printed when the following
program is compiled and run?

public class MyClass {


public static void main(String[] args) {
try {
f();
} catch (InterruptedException e) {
System.out.println("1");
throw new RuntimeException();
} catch (RuntimeException e) {
System.out.println("2");
return;
} catch (Exception e) {
System.out.println("3");
} finally {
System.out.println("4");
}
System.out.println("5");
}
// InterruptedException is a direct subclass of Exception.
static void f() throws InterruptedException {
throw new InterruptedException("Time for lunch.");
}
}

Select the one correct answer.

a. The program will print 5.


b. The program will print 1 and 4, in that order.
c. The program will print 1, 2, and 4, in that order.
d. The program will print 1, 4, and 5, in that order.
e. The program will print 1, 2, 4, and 5, in that order.
f. The program will print 3 and 5, in that order.

Question 37) Which digits, and in which order, will be printed when the following
program is run?

public class MyClass {


public static void main(String[] args) throws InterruptedException {
try {
f();
System.out.println("1");
} finally {
System.out.println("2");
}
System.out.println("3");
}
// InterruptedException is a direct subclass of Exception.
static void f() throws InterruptedException {
throw new InterruptedException("Time to go home.");
}
}

Select the one correct answer.

a. The program will print 2 and throw InterruptedException.


b. The program will print 1 and 2, in that order.
c. The program will print 1, 2, and 3, in that order.
d. The program will print 2 and 3, in that order.
e. The program will print 3 and 2, in that order.
f. The program will print 1 and 3, in that order

Question 38) What is wrong with the following code?

public class MyClass {


public static void main(String[] args) throws A {
try {
f();
} finally {
System.out.println("Done.");
} catch (A e) {
throw e;
}
}

public static void f() throws B {


throw new B();
}
}
class A extends Throwable {}

class B extends A {}

Select the one correct answer.

a. The main() method must declare that it throws B.


b. The finally block must follow the catch block in the main() method.
c. The catch block in the main() method must declare that it catches B rather than A.
d. A single try block cannot be followed by both a finally and a catch block.
e. The declaration of class A is illegal.

Question 39) Given the following interface definition, which definition is valid?

interface I {
void setValue(int val);
int getValue();
}

Select the correct answer(s).


a. class A extends I {
b. int value;
c. void setValue(int val) { value = val; }
d. int getValue() { return value; }
e. }
f.
g. interface B extends I {
h. void increment();
i. }
j.
k. abstract class C implements I {
l. int getValue() { return 0; }
m. abstract void increment();
n. }
o.
p. interface D implements I {
q. void increment();
r. }
s.
t. class E implements I {
u. int value;
v. public void setValue(int val) { value = val; }
w. }
Question 40) Given the following class definitions, which expression identifies
whether the object referred to by obj was created by instantiating class B rather
than classes A, C, and D?

class A {}
class B extends A {}
class C extends B {}
class D extends A {}

Select the one correct answer.

a. obj instanceof B
b. obj instanceof A && !(obj instanceof C)
c. obj instanceof B && !(obj instanceof C)
d. !(obj instanceof C || obj instanceof D)
e. !(obj instanceof A) && !(obj instanceof C) && !(obj instanceof D)

You might also like