Java Test 7 Aug 2k9
Java Test 7 Aug 2k9
Important Instructions
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);
}
Question 3)
class C1
{
static class C2
{
static int i1;
}
public static void main(String a[])
{
System.out.println(C1.C2.i1);
}
}
Question 4)
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?
A) wait()
B) run()
C) stop()
D) update()
E) resume()
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; }
}
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;
}
}
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
}}
Question 14)
abstract class A {} // 1
transient class B {} // 2
private class C {} // 3
static class D {} // 4
A) 1
B) 2
C) 3
D) 4
E) None of the above
Question 15) Select three correct statements about the following code.
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 18) Which of the following expressions will produce errors upon
compilation?
class Hmpf
{
//------
class Zing
{
protected Hmpf h;
}
//-------
Question 20) Given the following code, which line of code, inserted at indicated
position, will NOT compile?
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;
}
}
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" );
}
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();
a) Pine
b) Tree
c) Forest
d) Oops
e) (nothing printed)
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
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 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");
}
}}
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" ) ;
}
}
};
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.
(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
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);
}
}
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?
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?
Question 37) Which digits, and in which order, will be printed when the following
program is run?
class B extends A {}
Question 39) Given the following interface definition, which definition is valid?
interface I {
void setValue(int val);
int getValue();
}
class A {}
class B extends A {}
class C extends B {}
class D extends A {}
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)