Anda di halaman 1dari 31

Internal ExamVI, J2SE 1.

5
Part -I
Max Marks: 30
Convener: Dinesh Kumar
Important Instructions
This Question Paper contains a total of 30 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.
Trainees need to score minimum 50% marks to pass this exam.

Question 1) Given the following code


class B extends A
{
int getID()
{
return id;
}
}
class C
{
public int name;
}
class A
{
C c = new C();
public int id;
}
Which are TRUE about instances of the classes listed above?
A)
B)
C)
D)
E)

A
C
A
B
B

IS-A B
IS-A A
HAS-A C
HAS-A A
HAS-A C

Question 2) Given the following code


class A
{
public void baz()
{
System.out.println("A");
}
}
public class B extends A
{
public static void main(String [] args)
{
A a = new B();
a.baz();
}
public void baz()
{
System.out.println("B");
}
}
What is the output?
A)
B)
C)
D)

A
B
Compilation fails.
An exception is thrown at runtime.

Question 3) 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)
B)
C)
D)
E)
F)

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


protected int doStuff(int x, float y) {return 4; }
private int doStuff(int x, float y) {return 4; }
private int doStuff(int x, double y) { return 4; }
long doStuff(int x, float y) { return 4; }
int doStuff(float x, int y) { return 4; }

Question 4) Given the following code, what is the output?


public class TestPoly
{
public static void main(String [] args )
{
Parent p = new Child();
}
}
//-------------------------------------------class Parent
{
public Parent()
{
super();
System.out.print("
}
}

instantiate a parent

");

//--------------------------------------------

class Child extends Parent


{
public Child()
{
System.out.print("
}
}

A)
B)
C)
D)
E)
F)

instantiate a child

instantiate a child
instantiate a parent
instantiate a child
instantiate a parent
instantiate a parent
instantiate a child
Compilation fails.
An exception is thrown at runtime.

");

Question 5) Given the following code, what is the output?


public class TestPoly
{
public static void main(String [] args )
{
Parent p = new Child();
}
}
//-------------------------------------------class Parent
{
public Parent()
{
super();
System.out.print("
}
}

instantiate a parent

");

//-------------------------------------------class Child extends Parent


{
public Child()
{
System.out.print("
super();
}

instantiate a child

}
A)
B)
C)
D)
E)
F)

instantiate a child
instantiate a parent
instantiate a child
instantiate a parent
instantiate a parent
instantiate a child
Compilation fails.
An exception is thrown at runtime.

");

Question 6) Given the following code, what is the output?


class MySuper
{
public MySuper(int i)
{
System.out.print("super
}
}

" +

i +

//-------------------------------------------public class MySub extends MySuper


{
public MySub()
{
super(2);
System.out.print(" sub
}

");

public static void main(String [] args)


{
MySuper sup = new MySub();
}
}

A)
B)
C)
D)

sub
super 2
super 2
sub
Compilation fails.
An exception is thrown at runtime.

);

Question 7) Given the following code, what is the output?


public class ThreeConst
{
public static void main(String [] args)
{
new ThreeConst(4L);
}
public ThreeConst(int x)
{
this();
System.out.print(" " +
}
public ThreeConst(long x)
{
this((int) x);
System.out.print(" " +
}
public ThreeConst()
{
System.out.print("
}
}

A)
B)
C)
D)
E)
F)

4
4 8
8 4
8 4 no-arg
no-arg 8 4
Compilation fails.

(x * 2) +

x +

no-arg

);

");

);

Question 8) Given the following code, what is the output?


public class ThreeConst
{
public static void main(String [] args)
{
new ThreeConst();
}
public void ThreeConst(int x)
{
System.out.print(" " + (x * 2) +
}
public void ThreeConst(long x)
{
System.out.print(" " + x +
}
public void ThreeConst()
{
System.out.print(" no-arg
}
}
A)
B)
C)
D)
E)
F)

no-arg
8 4 no-arg
no-arg 8 4
Compilation fails.
No output is produced.
An exception is thrown at runtime.

);

");

);

Question 9) Given the following code


class Dog
{
Dog(String name)
{
}
}
If class Beagle extends Dog, and class Beagle has only one constructor,
which of the following could be the legal constructor for class Beagle?
A)
B)
C)
D)

Beagle() { }
Beagle() { super(); }
Beagle() { super("fido"); }
Do Not provide any explicit constructor, allow the default
constructor only.

Question 10) 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)
B)
C)
D)
E)

short y = 7;
int y = (int) 7.2d;
Byte y = 7;
char y = 's';
int y = 0xface;

Question 11). Which digits, and in which order, will be printed when the
following program is run?
public class MyClass {
public static void main(String[] args) {
int k=0;
try {
int i = 5/k;
} catch (ArithmeticException e) {
System.out.println("1");
} catch (RuntimeException e) {
System.out.println("2");
return;
} catch (Exception e) {
System.out.println("3");
} finally {
System.out.println("4");
}
System.out.println("5");
}
}
Select the one correct answer.
A)
B)
C)
D)
E)
F)

The
The
The
The
The
The

program
program
program
program
program
program

will
will
will
will
will
will

only
only
only
only
only
only

print
print
print
print
print
print

5.
1 and 4, in that order.
1, 2, and 4, in that order.
1, 4, and 5, in that order.
1, 2, 4, and 5, in that order.
3 and 5, in that order.

Question 12) Given the following code, which of the following code
fragments inserted at indicated position will NOT compile?
import java.util.*;
class Ro
{
public static void main(String... args)
{
Ro r = new Ro();
Object o = r.test();
}

Object test()
{
//insert code here
}
}
A) return null;
B) Object t = new Object();
return t;
C) int [] a = new int [2];
return a;
D) char [] [] c = new char [2][2];
return c[0] [1];
E) char [] [] c = new char [2][2];
return c[1];
F) return 7;

Question 13). What will be the result of attempting to compile and run the
following program?
public class MyClass {
public static void main(String[] args) {
RuntimeException re = null;
throw re;
}
}
Select the one correct answer.
A) The code will fail to compile, since the main() method does not declare
that it throws RuntimeException in its declaration.
B) The program will fail to compile, since it cannot throw re.
C) The program will compile without error and will throw
java.lang.RuntimeException when run.
D) The program will compile without error and will throw
java.lang.NullpointerException when run.
E) The program will compile without error and will run and terminate
without any output.

Question 14) Which statements are NOT true about super() or this()?
A) A super() or this()call must always be provided explicitly as the first
statement in the body of a constructor.
B) If both a subclass and its super-class do not have any declared
constructors, the implicit default constructor of the subclass will call
super()when run.
C) If neither super() or this() is declared as the first
statement in the body of a constructor, then this() will be
implicitly inserted as the first statement.
D) If super() is the first statement in the body of a constructor, then
this() can be declared as second statement.
E) Calling super() as the first statement in the body of a constructor of a
subclass will always work, since all super-classes
have default
constructor.

Question 15). 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 16) For the class Employee given in above Question # 15, what is
the output when following code is executed?
public class ParamTest
{
public static void main(String[] args)
{
Employee a = new Employee("Alice", 70000);
Employee b = new Employee("Bob", 60000);
swap(a, b);
System.out.print(a.getName()+ );
System.out.print(b.getName()+ );
}
public static void swap(Employee x, Employee y)
{
Employee temp = x;
x = y;
y = temp;
System.out.print(x.getName()+ );
System.out.print(y.getName() + );
}
}

A)
B)
C)
D)
E)
F)

Compilation fails.
Exception is generated at runtime
Alice Bob Alice Bob
Bob Alice Bob Alice
Bob Alice Alice Bob
Alice Alice Bob Bob

Question 17) What is the output when following code is executed?


class A
{
public void show()
{
System.out.print( A );
}
}
class B extends A
{
public void show()
{
System.out.print( B );
}
}
class C extends B
{
public void show()
{
System.out.print( C );
}
}
public class Test
{
public static void main(String args[ ])
{
A a = new C();
a.show();
B b = new C();
b.show();
C c = new C();
c.show();
A d = new B();
d.show();
}
}
A)
B)
C)
D)
E)
F)
G)
H)

Compilation fails.
Exception is generated at runtime
A B C A
C C A B
A C A C
A B B A
C C C B

Question 18) Given the following code, what is the output?


class A
{
public final void show(A a)
{
System.out.print( A );
}
}
class B extends A
{
public final void show(B b)
{
System.out.print( B );
}
}
class C extends B
{
public final void show(C c)
{
System.out.print( C );
}
}
public class Test
{
public static void main(String args[ ])
{
A a = new C();
a.show(new C() );
B b = new C();
b.show(new B() );
C c = new C();
c.show(new C() );
A d = new B();
d.show(new B() );
}
}
A)
B)
C)
D)
E)
F)
G)

Compilation fails.
Exception is generated at runtime
A B C A
B C A B
A B A C
A B B A
A B C B

Question 19) Given following code what is the output?


class Clidders
{
public final void flipper()
{
System.out.print(" Clidder
}
}

");

//--------------public class Clidlets extends Clidders


{
public void flipper()
{
System.out.print(" Flip a Clidlet
super.flipper();
}
public static void main(String [] args)
{
new Clidlets().flipper();
}
}

A. Flip a Clidlet
B. Flip a Clidder
C. Flip a Clidder

Flip a Clidlet

D. Flip a Clidlet

Flip a Clidder

E. Compilation fails.

");

Question 20. Given the following,


1. System.out.print("Start ");
2. try {
3. System.out.print("Hello world");
4. throw new FileNotFoundException();
5. }
6. System.out.print(" Catch Here ");
7. catch(EOFException e) {
8. System.out.print("End of file exception");
9. }
10. catch(FileNotFoundException e) {
11. System.out.print("File not found");
12. }
and given that EOFException and FileNotFoundException are both
subclasses of IOException, and further assuming this block of code is
placed into a class, which statement is most true concerning this code?
A)
B)
C)
D)

The code will not compile.


Code output: Start Hello world File Not Found.
Code output: Start Hello world End of file exception.
Code output: Start Hello world Catch Here File not found.

Question 21) Given the following code, what is the output?


class Clidder
{
private final void flipper()
{
System.out.println ("Clidder");
}
}
public class Clidlet extends Clidder
{
public final void flipper()
{
System.out.println("Clidlet");
}

public static void main(String [] args)


{
new Clidlet().flipper();
}

A. Clidlet
B. Clidder
C. Clidder

Clidlet

D. Clidlet

Clidder

E. Compilation fails.

Question 22) Given the following code, which statement(s), inserted at


indicated position, will compile successfully?
class Plant
{
String getName()
{
return "plant";
}
Plant getType()
{
return this;
}
}
//--------------class Flower extends Plant
{
// -- position --insert code here
}
//--------------class Tulip extends Flower
{
}

A. Flower getType() { return this; }


B. String getType() { return "this"; }
C. Plant getType() { return this; }
D. Tulip getType() { return new Tulip() ;}

Question 23. Given the following code


1. public class MyProgram {
2. public static void throwit() {
3. throw new RuntimeException();
4. }
5. public static void main(String args[]){
6. try {
7. System.out.println("Hello world ");
8. throwit();
9. System.out.println("Done with try block ");
10. }
11. finally {
12. System.out.println("Finally executing ");
13. }
14. }
15. }
Which answer most closely indicates the behavior of the program?
A) The program will not compile.
B) The program will print Hello world, then will print that a
RuntimeException has occurred, then will print Done with try block, and
then will print Finally executing.
C) The program will print Hello world, then will print that a
RuntimeException has occurred, and then will print Finally executing.
D) The program will print Hello world, then will print Finally executing, then
will print that a RuntimeException has occurred.

Question 24) Given the following code, which statement(s), inserted at


indicated position, will compile successfully?
class Programmer
{
Programmer debug()
{
return this;
}
}
//------------class SCJP extends Programmer
{
// insert code here
}
A. Programmer debug() { return this; }
B. SCJP debug() { return this; }
C. Object debug() { return this; }
D. int debug() { return 1; }
E. int debug(int x) { return 1; }
F. Object debug (int x) { return this; }

Question 25. Given the following code


1. public class Switch2 {
2. final static short x = 2;
3. public static int y = 0;
4. public static void main(String [] args) {
5. for (int z=0; z < 3; z++) {
6. switch (z) {
7. case y: System.out.print("0 ");
8. case x-1: System.out.print("1 ");
9. case x: System.out.print("2 ");
10. }
11. }
12. }
13. }
What is the result?
A)
B)
C)
D)
E)
F)

012
012122
Compilation fails at line 7.
Compilation fails at line 8.
Compilation fails at line 9.
An exception is thrown at runtime.

Question 26) Given the following code, which statement(s), inserted at


indicated position, will compile successfully?
class Dog
{
}
//-----class Beagle extends Dog
{
}
//-------class Kennel
{
public static void main(String [] arfs)
{
Beagle bl = new Beagle();
Dog dogl = new Dog();
Dog dog2 = bl;
// insert code here
}
}
A. Beagle b2 = (Beagle) dog1;
B. Beagle b3 = (Beagle) dog2;
C. Beagle b4 = dog2;
D. None of the above statements will compile.

Question 27) Given the following code, which statement(s), inserted at


indicated position, will compile successfully?
class X
{

void dol()
{
}

}
//------class Y extends X
{
void do2()
{
}
}
//-------class Chrome
{
public static void main(String [] args)
{
X x1 = new X();
X x2 = new Y();
Y y1 = new Y();
// insert code here
}
}
A. x2.do2( );
B. (Y) x2. do2( );
C. ((Y)x2).do2();
D. None of the above statements will compile.

Question 28) Given the following code, what is the output?


class Fizz
{
int x = 5;
public static void main(String... args)
{
final Fizz f1 = new Fizz();
Fizz f2 = new Fizz();
Fizz f3 = FizzSwitch(f1,f2);
System.out.println((f1 == f3) + " " + (f1.x == f3.x));
}
static Fizz FizzSwitch(Fizz x, Fizz y)
{
final Fizz z = x;
z.x = 6;
return z;
}
}
What is the result?
A)
B)
C)
D)
E)
F)

true true
false true
true false
false false
Compilation fails.
An exception is thrown at runtime.

Question 29) Given the following code, what is the output?


class Knowing
{
static final long tooth = 343L;
static long doIt(long tooth)
{
System.out.print(++tooth + " ");
return ++tooth;
}
public static void main(String[] args)
{
System.out.print(tooth + " ");
final long tooth = 340L;
new Knowing().doIt(tooth);
System.out.println(tooth);
}
}
What is the result?
A)
B)
C)
D)
E)
F)
G)

343 340 340


343 340 342
343 341 342
343 341 340
343 341 343
Compilation fails.
An exception is thrown at runtime.

Question 30) Consider the following code, with line numbers prefixed on
every line, watch carefully the case-sensitivity

1. class Convert {
2.
public static void main(String[] args) {
3.
Long xL = new Long(456L);
4.
long x1 = Long.valueOf("123");
5.
Long x2 = Long.valueOf("123");
6.
long x3 = xL.longValue();
7.
Long x4 = xL.longValue();
8.
Long x5 = Long.parseLong("456");
9.
long x6 = Long.parseLong("123");
10. }
11. }
Which lines of code will compile using Java 5, but will NOT compile using
Java 1.4?
A)
B)
C)
D)
E)
F)

Line
Line
Line
Line
Line
Line

4.
5.
6.
7.
8.
9.

!! End of Exam !!

Anda mungkin juga menyukai