Sie sind auf Seite 1von 14

Mock Questions 1

Question 1.
Consider the following array definitions:
int[] array1, array2[];
int[][] array3;
int[] array4[], array5[];
Which of the follwing are valid statements?

Select 3 correct options


a array2 = array3;

b array2 = array4;

c array1 = array2;

d array4 = array1;

e array5 = array3

Question 2.
Which statements concerning the value of a member variable are true, when it is not initialized explicitly?

Select 1 correct option.


a The value of a String variable is "" (empty string).

b The value of all numeric types is zero and of char is '0'.

c boolean variable will remain uninitialized.


d boolean variable will be initialized to true.

e The value of all object variables is null.

Question 3.
Given the following class, which statements can be inserted at line 1 without causing the code to fail
compilation?

public class TestClass


{
int a;
int b = 0;
static int c;
public void m()
{
int d;
int e = 0;
// Line 1
}
}

Select 4 correct options


a a++;

b b++;

c c++;

d d++;

e e++;

Question 4.
Consider following two classes:
What will be the output of compiling and running class B ?

//in file A.java


package p1;
public class A
{
protected int i = 10;
public int getI() { return i; }
}
//in file B.java
package p2;
import p1.*;
public class B extends p1.A
{
public void process(A a)
{
a.i = a.i*2;
}
public static void main(String[] args)
{
A a = new B();
B b = new B();
b.process(a);
System.out.println( a.getI() );
}
}

Select 1 correct option.


a It will print 10.

b It will print 20.

c It will not compile.

d It will throw an exception at Run time.

e None of the above.

Question 5.
Which line contains a valid constructor in the following class definition?
public class TestClass
{
int i, j;
public TestClass getInstance() { return new TestClass(); } //1
public void TestClass(int x, int y) { i = x; j = y; } //2
public TestClass TestClass() { return new TestClass(); } //3
public ~TestClass() { i = x; j = y; } //4
}

Select 1 correct option.


a Line 1

b Line 2

c Line 3

d Line 4

e None of the above.

Question 6
Which lines contain a valid constructor in the following code?

public class TestClass


{
public TestClass(int a, int b) { } // 1
public void TestClass(int a) { } // 2
public TestClass(String s); // 3
private TestClass(String s, int a) { } //4
public TestClass(String s1, String s2) { }; //5
}

Select 3 correct options


a Line // 1

b Line // 2

c Line // 3

d Line // 4

e Line // 5

Question 7
Compared to public, protected and private accessibility, default accessibility is....

Select 1 correct option.


a Less restrictive than public

b More restrictive than public, but less restrictive than protected.

c More restrictive than protected, but less restrictive than private.

d More restrictive than private.


e Less restrictive than protected from within a package, and more restrictive than protected from outside a
package.

Question 8
How can you declare 'i' so that it is not visible outside the package 'test'.

package test;
class Test
{
XXX int i;
/* lot of code */
}

Select 2 correct options


a private
Note that, it does not say that 'x' should be accessible from this package.

b public
It will make it available to everybody.

c protected
It will make it available to a subclass even if the subclass is in a different package.

d No access modifier

e friend
There is no such modifier in Java.

Question 9
Consider the following program...

class ArrayTest
{
public static void main(String[] args)
{
int ia[][] = { {1, 2}, null };
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
System.out.println(ia[i][j]);
}
}

Which of the following statements are true?

Select 1 correct option.


a It will not compile.

b It will throw an ArrayIndexOutOfBoundsException at Runtime.

c. It will throw a NullPointerException at Runtime.

d It will compile and run without throwing any exceptions.

e None of the above.


Question 10
Which of the following options can be a part of a correct inner class declaration or a combined declaration
and instance initialization ?

Select 2 correct options


a private class C

b new SimpleInterface() { ... }

c new ComplexInterface(x) { ... }

d private final abstract class C

e new ComplexClass() implements SimpleInterface

Question 11
Consider the following classes...
(See Exhibit)
Which of the following method declarations would be valid at line //1 ?

class Teacher
{
void teach(String student)
{
/* lots of code */
}
}
class Prof extends Teacher
{
//1
}

Select 4 correct options


a public void teach() throws Exception

b private void teach(int i) throws Exception

c protected void teach(String s)

d public final void teach(String s)

e public abstract void teach(String s)

Question 12
What is the correct declaration of an abstract public method 'add' accessible to everybody, takes no
arguments and returns nothing?
(Use only one space between words)

Correct Answer(s)
a. public abstract void add();

b. abstract public void add()


c. public abstract void add()

d. abstract public void add();

Question 13
Which of the following statements is correct?

Select 1 correct option.


a new, delete and goto are keywords in the Java language

b try, catch and thrown are keywords in the Java language

c static, unsigned and long are keywords in the Java language

d exit, class and while are keywords in the Java language

e return, goto and default are keywords in the Java language

Question 14
Consider the following class...

What will happen when you attempt to compile and run the program?

import java.awt.*;
import java.awt.event.*;
class TestFrame extends Frame
{
String s="Message";
public static void main(String args[])
{
TestFrame t = new TestFrame();
Button b = new Button("press me");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Message is " +s);
}
}
);
t.add(b);
}
}

Select 1 correct option.


a It will not compile.

b It will compile but not show anything when run.

c It will compile and show a very small Frame.

d It will compile and show a frame big enough to display the button.
e None of the above.

Question 15
The following is a valid member variable declaration ...

private static final transient int i = 20;

Select 1 correct option.


a True

b False

Question 16
Which statement regarding the following code is true?

int a, b;
b = 5;

Select 1 correct option.


a Variable 'a' is not declared

b Variable 'b' is not declared

c Variable 'a' is declared but not initialized

d Variable 'b' is declared but not initialized.

e Variable 'b' is initialized but not declared.

Question 17
Which one of these is a proper definition of a class TestClass that cannot be sub-classed?

Select 1 correct option.


a final class TestClass { }

b abstract class TestClass { }

c native class TestClass { }

d static class TestClass { }

e private class TestClass { }

Question 18
What will the following program print ?(See Exhibit)

class Test
{
public static void main(String[] args)
{
int i = 4;
int ia[][][] = new int[i][i = 3][i];
System.out.println( ia.length + ", " + ia[0].length+", "+ ia[0][0].length);
}
}

Select 1 correct option.


a It will not compile.

b 3, 4, 3

c 3, 3, 3

d 4, 3, 4

e 4, 3, 3

Question 19
Which of these are not legal declarations within a class?

Select 1 correct option.


a static volatile int sa ;

b final Object[ ] objArr = { null } ;

c abstract int t ;

d native void format( ) ;

e final transient static private double PI = 3.14159265358979323846 ;

Question 20
Which of the following are valid declarations of the standard main() method?

Select 2 correct options


a static void main(String args[ ]) { }

b public static int main(String args[ ]) {}

c public static void main (String args) { }

d final static public void main (String[ ] arguments ) { }

e public static void main (String[ ] args) { }

Question 21
What, if anything, is wrong with the following code?

abstract class TestClass


{
transient int j;
synchronized int k;
final void TestClass(){}
static void f()
{
k = j++;
}
}

Select 2 correct options


a The class TestClass cannot be declared abstract.

b The variable j cannot be declared transient.

c The variable k cannot be declared synchronized.

d The constructor TestClass( ) cannot be declared final.

e The method f( ) cannot be declared static.

Question 22
Which line, if any, will give a compile time error ?

void test(byte x)
{
switch(x)
{
case 'a': // 1
case 256: // 2
case 0: // 3
default : // 4
case 80: // 5
}
}

Select 1 correct option.


a Line 1 as 'a' is not compatible with byte.

b Line 2 as 256 cannot fit into a byte.

c No compile time error but a run time error at line 2.

d Line 3 as the default label must be the last label in the switch statement.

e There is nothing wrong with the code.

Question 23
What is the correct parameter specification for the standard main method?

Select 2 correct options


a void

b String[ ] args

c Strings args[ ]
d String args

e String args[ ]

Question 24
Which of these combinations of switch expression types and case label value types are legal within a switch
statement?

Select 2 correct options


a switch expression of type int and case label value of type char.

b switch expression of type float and case label value of type int.

c switch expression of type byte and case label value of type float.

d switch expression of type char and case label value of type byte.

e switch expression of type boolean and case label value of type boolean.

Question 25
Consider:

public class Outer


{
int i = 10;
class Inner
{
public void methodA()
{
//line 1.
}
}
}

Which of the following statements are valid at line 1 (Select the best answer).

Select 2 correct options


a System.out.println(this.i);

b System.out.println(i);

c System.out.println(Outer.this.i);

d 'i' cannot be accessed inside the inner class method.

Question 26
Which of these statements are true?

Select 2 correct options


a Abstract class can contain final methods.
b Variables can be declared native.

c Non-abstract methods can be declared in abstract classes.

d Classes can be declared native.

e Abstract classes can be final.

Question 27
Given the following class, which of these are valid ways of referring to the class from outside of the
package com.enthu?

package com.enthu;
public class Base
{
// ....
// lot of code...
}

Select 2 correct options


a Base

b By importing the package com.* and referring to the class as enthu.Base

c importing com.* is illegal.

d By importing com.enthu.* and referring to the class as Base.

e By referring to the class as com.enthu.Base.

Question 28
Which of these statements are true?

Select 2 correct options


a A static method can call other non-static methods in the same class by using the 'this' keyword.

b A class may contain both static and non-static variables and both static and non-static methods. *

c Each object of a class has its own instance of each non-static member variable. *

d Instance methods may access local variables of static methods.

e All methods in a class are implicitly passed a 'this' parameter when called.

Question 29
Which of the following is not a legal Java identifier?

Select 1 correct option.


a num

b int123

c 2Next
d _interface

e a$_123

Question 30
For object o1 of class A to access a member(field or method) of object o2 of class B, when the member has
no access modifier, class B must be...

Select 1 correct option.


a a Subclass of A

b in the same package as A is in.

c a Super class of A

d a subclass but may not be in the same package.

e in the same package and must be a Subclass of A.

Question 31
Given the following classes and declarations, which of these statements about //1 and //2 are true?
(See Exhibit)

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
}

Select 1 correct option.


a System.out.println(b.i); will print 10.

b The statement b.f( ); will give compile time error..

c System.out.println(b.i); will print 20

d All the above are correct.

e None of the above statements are correct.

Question 32
Which of the following code fragments are valid method declarations?
Select 1 correct option.
a void method1{ }

b void method2( ) { }

c void method3(void){ }

d method4{ }

e method5(void){ }

Question 33
Which of the following code fragments will successfully initialize a two-dimensional array of chars named
cA with a size such that cA[2][3] refers to a valid element?

1. char[][] cA = { { 'a', 'b', 'c' }, { 'a', 'b', 'c' } };

2. char cA[][] = new char[3][];


for (int i=0; i<cA.length; i++) cA[i] = new char[4];

3. char cA[][] = { new char[ ]{ 'a', 'b', 'c' } , new char[ ]{ 'a', 'b', 'c' } };

4. char cA[3][2] = new char[][] { { 'a', 'b', 'c' }, { 'a', 'b', 'c' } };

5. char[][] cA = { "1234", "1234", "1234" };

Select 1 correct option.


a 1, 3

b 4, 5

c 2, 3

d 1, 2, 3

e 2

Question 34
Which of the following correctly declare a variable which can hold an array of 10 integers?

Select 2 correct options


a int[ ] iA

b int[10] iA

c int iA[ ]

d Object[ ] iA

e Object[10] iA

Question 35
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
}
}

Select 3 correct options


a i = this.i1;

b i = this.i2;

c this = new Test( );

d this.i = 4;

e this.i1 = i2;

Das könnte Ihnen auch gefallen