wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Theory Quiz

Total questions: 32

Worksheet time: 1hrs 6mins

Name
Class
Date
1.

How are interfaces different from abstract classes?

a)

Interfaces can have constructors, while abstract classes cannot.

b)

Abstract classes can contain both abstract and concrete methods, while interfaces can only contain abstract methods.

c)

Interfaces cannot be extended by other interfaces, while abstract classes can.

d)

Abstract classes can have instance variables, while interfaces cannot.

2.

Which of the following best describes an interface in Java?

a)

A blueprint for a class that cannot be instantiated.

b)

A concrete implementation of a class with predefined methods.

c)

A special type of class that can only contain static methods.

d)

A type of variable used for method overloading.

3.

Which keyword is used to declare an interface in Java?

a)

interface

b)

class

c)

abstract

d)

implements

4.

How can a class in Java realize an interface?

a)

By using the extends keyword.

b)

By using the implements keyword followed by the interface name.

c)

By directly inheriting from the interface class.

d)

By using the implements keyword followed by the class name.

5.

Which of the following is true about interfaces in Java?

a)

A class can implement multiple interfaces.

b)

An interface can implement another interface.

c)

An interface can extend a class.

6.

Which of the following statements about interface variables is true?

a)

Interface variables are implicitly final and static.

b)

Interface variables can be reassigned new values after declaration.

c)

Interface variables can only be accessed through static methods.

d)

Interface variables can have different access modifiers depending on the implementing class.

7.

Can an interface in Java contain fields (variables)?

a)

Yes, but only if they are declared as static final.

b)

Yes, but only if they are declared as private.

c)

No, interfaces cannot contain fields.

d)

No, fields are not allowed in any interface.

8.

Which of the following statements about interface inheritance is true?

a)

An interface can inherit from multiple classes.

b)

A class can inherit from multiple interfaces.

c)

An interface cannot inherit from another interface.

d)

An interface can inherit from both classes and other interfaces.

9.

What key words are implied when we write methods for an interface?

a)

public

b)

abstract

c)

static

d)

void

10.

Can an interface extend a class in Java?

a)

Yes

b)

No

c)

Only if the class is declared as final.

d)

Only if the class is declared as abstract.

11.

Which of the following is true about method overriding in Java interfaces?

a)

Methods in interfaces cannot be overridden.

b)

Methods in interfaces are final and cannot be overridden.

c)

Classes implementing interfaces can override methods defined in the interface.

12.

Which of the following is a valid way to declare a constant in an interface?

a)

final int MY_CONSTANT;

b)

private static final int MY_CONSTANT = 10;

c)

public static int MY_CONSTANT = 10;

d)

public static final int MY_CONSTANT = 10;

13.

Which of the following will be the output of the above program?

public class Compute {

public static void main (string args [ ]){

int result, x ;

x = 1 ;

result = 0;

while (x < = 10) {

if (x%2 == 0)

result + = x ;

+ + x ;

}

System.out.println(result) ;

}

}

a)

55

b)

30

c)

25

d)

35

14.

class Test {

public static void main(String[] args){

int i = 0, j = 9;

do {

i++;

if (j-- < i++) {

break;

}

} while (i < 5);

System.out.println(i + "" + j);

}

}

a)

44

b)

55

c)

66

d)

77

15.

public class Test{

public static void main(String args[]){

int i = 0, j = 5 ;

for( ; (i < 3) && (j++ < 10) ; i++ ){

System.out.print(" " + i + " " + j );

}

System.out.print(" " + i + " " + j );

}

}

a)

0 6 1 7 2 8 3 8

b)

0 6 1 7 2 8 3 9

c)

0 6 1 5 2 5 3 5

d)

Compilation Error

16.

class Test{

public static void main(String args[]){

int x=7;

if(x==2); // Note the semicolon

System.out.println("NumberSeven");

System.out.println("NotSeven");

}

}

a)

NumberSeven NotSeven

b)

NumberSeven

c)

NotSeven

d)

Error

17.

public class Test{

public static void main(String args[]){

int i, j;

for(i=1, j=0;i<10;i++) j += i;

System.out.println(i);

}

}

a)

10

b)

11

c)

9

d)

20

18.

public class Test{

public static void main(String[] args){

double sum = 0;

for(double d = 0; d < 10;){

d += 0.1;

sum += sum + d;

}

}

}

a)

The program has a compile error because the adjustment is missing in the for loop.

b)

The program has a compile error because the control variable in the for loop cannot be of the double type.

c)

The program runs in an infinite loop because d<10 would always be true.

d)

The program compiles and runs fine.

19.

Which of the following for loops will be an infinite loop?

a)

for(; ;)

b)

for(i=0 ; i<1; i--)

c)

for(i=0; ; i++)

d)

All of the above

20.

What is the value of a[1] after the following code is executed?

int[] a = {0, 2, 4, 1, 3};

for(int i = 0; i < a.length; i++)

a[i] = a[(a[i] + 3) % a.length];

a)

0

b)

1

c)

2

d)

3

21.
Given the nested if-else structure above, If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed?
a)
0
b)
2
c)
3
d)
4
22.
Given the nested if-else structure above, if x is currently 0, a = 0 and b = -5, what will x become after the above statement is executed?
a)
0
b)
2
c)
3
d)
4
23.
Given the nested if-else structure above, if x is currently 0, a = 1 and b = -1, what will x become after the above statement is executed?
a)
0
b)
2
c)
3
d)
5
24.
Consider the above code that will assign a letter grade of ‘A’, ‘B’, ‘C’, ‘D’, or ‘F’ depending on a student’s test score.  
a)
This code will work correctly in all cases
b)
This code will work correctly only if grade >= 60
c)
This code will work correctly only if grade < 60
d)
This code will work correctly only if grade < 70
25.
If x is an int where x = 1, what will x be after the above loop terminates?
a)
64
b)
100
c)
128
d)
None of the above, this is an infinite loop
26.
If x is an int where x = 0, what will x be after the following loop terminates?
a)
64
b)
100
c)
128
d)
None of the above, this is an infinite loop
27.
Given the above code, where x = 0, what is the resulting value of x after the for-loop terminates?
for(int i=0;  i<5; i++)    
            x += i;
a)
4
b)
5
c)
10
d)
15
28.
How many times will the above loop iterate?
a)
0
b)
9
c)
10
d)
11
29.
The above nested loop structure will execute the inner most statement (x++) how many times?
a)
100
b)
200
c)
10000
d)
20000
30.

In total, how many times is the inner loop executed?

a)

5

b)

10

c)

15

d)

50

e)

Infinite Loop

31.

Which of the following for loop declaration is not valid?

a)

for ( int i = 99; i >= 0; i / 9 )

b)

for ( int i = 7; i <= 77; i += 7 )

c)

for ( int i = 20; i >= 2; - -i )

d)

for ( int i = 2; i <= 20; i = 2* i )

32.

What will be the output of the following program?


public class Test

{

public static void main(String[] args)

{

int count = 1;

while (count <= 15)

{ System.out.println(count % 2 == 1 ? "***" : "+++++"); ++count;

} // end while

} // end main

}

a)

15 times ***

b)

15 times +++++

c)

8 times *** and 7 times +++++

d)

Both will print only once