Font size
WorksheetsTheory Quiz
Total questions: 32
Worksheet time: 1hrs 6mins
How are interfaces different from abstract classes?
Interfaces can have constructors, while abstract classes cannot.
Abstract classes can contain both abstract and concrete methods, while interfaces can only contain abstract methods.
Interfaces cannot be extended by other interfaces, while abstract classes can.
Abstract classes can have instance variables, while interfaces cannot.
Which of the following best describes an interface in Java?
A blueprint for a class that cannot be instantiated.
A concrete implementation of a class with predefined methods.
A special type of class that can only contain static methods.
A type of variable used for method overloading.
Which keyword is used to declare an interface in Java?
interface
class
abstract
implements
How can a class in Java realize an interface?
By using the extends keyword.
By using the implements keyword followed by the interface name.
By directly inheriting from the interface class.
By using the implements keyword followed by the class name.
Which of the following is true about interfaces in Java?
A class can implement multiple interfaces.
An interface can implement another interface.
An interface can extend a class.
Which of the following statements about interface variables is true?
Interface variables are implicitly final and static.
Interface variables can be reassigned new values after declaration.
Interface variables can only be accessed through static methods.
Interface variables can have different access modifiers depending on the implementing class.
Can an interface in Java contain fields (variables)?
Yes, but only if they are declared as static final.
Yes, but only if they are declared as private.
No, interfaces cannot contain fields.
No, fields are not allowed in any interface.
Which of the following statements about interface inheritance is true?
An interface can inherit from multiple classes.
A class can inherit from multiple interfaces.
An interface cannot inherit from another interface.
An interface can inherit from both classes and other interfaces.
What key words are implied when we write methods for an interface?
public
abstract
static
void
Can an interface extend a class in Java?
Yes
No
Only if the class is declared as final.
Only if the class is declared as abstract.
Which of the following is true about method overriding in Java interfaces?
Methods in interfaces cannot be overridden.
Methods in interfaces are final and cannot be overridden.
Classes implementing interfaces can override methods defined in the interface.
Which of the following is a valid way to declare a constant in an interface?
final int MY_CONSTANT;
private static final int MY_CONSTANT = 10;
public static int MY_CONSTANT = 10;
public static final int MY_CONSTANT = 10;
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) ;
}
}
55
30
25
35
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);
}
}
44
55
66
77
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 );
}
}
0 6 1 7 2 8 3 8
0 6 1 7 2 8 3 9
0 6 1 5 2 5 3 5
Compilation Error
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");
}
}
NumberSeven NotSeven
NumberSeven
NotSeven
Error
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);
}
}
10
11
9
20
public class Test{
public static void main(String[] args){
double sum = 0;
for(double d = 0; d < 10;){
d += 0.1;
sum += sum + d;
}
}
}
The program has a compile error because the adjustment is missing in the for loop.
The program has a compile error because the control variable in the for loop cannot be of the double type.
The program runs in an infinite loop because d<10 would always be true.
The program compiles and runs fine.
Which of the following for loops will be an infinite loop?
for(; ;)
for(i=0 ; i<1; i--)
for(i=0; ; i++)
All of the above
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];
0
1
2
3
for(int i=0; i<5; i++)
x += i;
In total, how many times is the inner loop executed?
5
10
15
50
Infinite Loop
Which of the following for loop declaration is not valid?
for ( int i = 99; i >= 0; i / 9 )
for ( int i = 7; i <= 77; i += 7 )
for ( int i = 20; i >= 2; - -i )
for ( int i = 2; i <= 20; i = 2* i )
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
}
15 times ***
15 times +++++
8 times *** and 7 times +++++
Both will print only once
