You are on page 1of 4

JAVA Test 1

Time = 40 min.
Marks= 1*9= 9 , 2*3= 6

1. Consider the code given below


class Animal
{

}
public class Cat extends Animal
{

}

Which of the statements are true about the code


a. A few members can be declared in class Cat
b. It does not inherit from the Object class
c. Cat class has access to the private members of the Animal class.
d. All of the above

2. super()is used to

a. invoke the no argument constructor of the super class


b. invoke the overridden method of the super class
c. invoke the parametrized constructor of the super class
d. None of the above

3. Which of the following statements are true about abstract classes

a. They can be inherited


b. They can contain only abstract methods
c. They can contain abstract and concrete methods
d. Both a and c

4. Which of the following is a valid array declaration statement?


A int a[] = new int[10]; B .int a[]= new int {1,2,3}; C . int a; D. None of the above

5. Which of the following methods is called by the garbage collector before the object is
garbage collected?

a. Finally b finalize() C .gc() D . All of the above


6. What gets printed when the following code is compiled and run. Select the one correct
answer. public class test {

public static void main(String args[]) {


int i = 1;
do {
i--;
} while (i > 2);
System.out.println(i);
}
}
A. 0 B .1 C . 2 D -1

7. What gets printed when the following program is compiled and run? Select the one correct
answer.

class test {

public static void main(String args[]) {

int i;

do {

i++;

while(i < 0);

System.out.println(i);

a. The program does not compile.


b. The program compiles but does not run.
c. The program compiles and runs but does not print anything.
d. The program prints 0.
8. What gets printed when the following program is compiled and run? Select the one correct
answer.

class xyz {

static int i;

public static void main(String args[]) {

while (i < 0) {

i--;

}
System.out.println(i);
}

a. The program does not compile. B. The program compiles but does not run.

C . The program compiles and runs but does not print anything. D. The program prints 0.
9. Given the following declarations, which of the assignments given in the options below would
compile. Select the two correct answers.

int i = 5;

boolean t = true;

float f = 2.3F;

double d = 2.3;

a. t = (boolean) i;
b. f = d;
c. d = i;
d. i = 5;
e. f = 2.8;

10 . Which declaration of the main method below would allow a class to be started as a
standalone program. Select the one correct answer.

A. public static int main(char args[])


B. public static void main(String cmds[])
C. public static void MAIN(String args[])
D. public static void main(string args)

11. Which of the following is true. Select the two correct answers.

A. A class that is abstract can not be instantiated.


B. The final keyword indicates that the body of a method is to be found elsewhere.
The code is written in non-Java language, typically in C/C++.
C. A static variable indicates there is only one copy of that variable.
D. A data member defined as private is neither inherited nor is accessible by sub
class.

12. Which of the following are legal array declarations. Select the three correct answers.
a. int i[5][];
b. int i[][];
c. int []i[];
d. int i[5][5];
e. int[][] a;
13. Which of the following are legal identifier names in Java. Select the two correct answers.
a. %abcd
b. $abcd
c. 1abcd
d. package

_a_long_name
14. What all gets printed when the following code is compiled and run? Select the three
correct answers.

public class xyz {

public static void main(String args[]) {


for(int i = 0; i < 2; i++) {
for(int j = 2; j>= 0; j--) {
if(i == j) break;
System.out.println("i=" + i + " j="+j);
}
}
}
}

a. i=0 j=0
b. i=0 j=1
c. i=0 j=2
d. i=1 j=0
e. i=1 j=1
f. i=1 j=2
g. i=2 j=0
h. i=2 j=1
i. i=2 j=2
15. Name the access modifier which when used with a method, makes it available to all the
classes in the same package and to all the subclasses of the class.
a. Public
b. Protected
c. Private
d. package

Descriptive Question:

1. Design an abstract class called Shape which contains an abstract method getArea().
Design two derived classes Reactangle and Circle and get their areas using dynamic
polymorphism.

2. Design an interface MathProcessor which has methods add(), subtract(), multiply().


Design a class Calculator which implements MathProcessor to do the addition,
subtractionand multiplication. The numbers to be operated upn and the operator should
be taken from the command line arguments.

You might also like