1.
What feedback do you have for this quiz, if any?
2.
How were you able to answer the previous question? Did you know the answer, take a guess, etc.?
3.
How were you able to answer the previous question? Did you know the answer, take a guess, etc.?
4.
How were you able to answer the previous question? Did you know the answer, take a guess, etc.?
5.
How were you able to answer the previous question? Did you know the answer, take a guess, etc.?
6.
How were you able to answer the previous question? Did you know the answer, take a guess, etc.?
7.
How were you able to answer the previous question? Did you know the answer, take a guess, etc.?
8.
How were you able to answer the previous question? Did you know the answer, take a guess, etc.?
9.
How were you able to answer the previous question? Did you know the answer, take a guess, etc.?
10.
How were you able to answer the previous question? Did you know the answer, take a guess, etc.?
11.
How were you able to answer the previous question? Did you know the answer, take a guess, etc.?
12.
How were you able to answer the previous question? Did you know the answer, take a guess, etc.?
13.
How were you able to answer the previous question? Did you know the answer, take a guess, etc.?
14.
How were you able to answer the previous question? Did you know the answer, take a guess, etc.?
15.
How were you able to answer the previous question? Did you know the answer, take a guess, etc.?
16.
How were you able to answer the previous question? Did you know the answer, take a guess, etc.?
17.
Is the following statement true or false? The constructor of a class must not have a return type.
Correct Answer
A. True
Explanation
The statement is true. In object-oriented programming, a constructor is a special method used to initialize objects of a class. It is called automatically when an object is created. Unlike other methods, a constructor does not have a return type, not even void. Its purpose is to set the initial values of the object's attributes and prepare it for use.
18.
What is the number of bytes used by Java primitive long?
Correct Answer
D. 8
Explanation
The correct answer is 8. In Java, the primitive data type "long" is a 64-bit signed two's complement integer. Since 1 byte is equal to 8 bits, a "long" variable occupies 8 bytes of memory. Therefore, the number of bytes used by a Java primitive long is 8.
19.
Which of the following is true?
Correct Answer
A. && operator is used for short-circuited logical AND.
Explanation
The explanation for the given correct answer is that the && operator in Java is used for short-circuited logical AND. This means that if the first operand of the && operator evaluates to false, the second operand is not evaluated because the overall result will always be false. This behavior is useful in situations where the second operand may have side effects or be computationally expensive, as it allows for efficient evaluation of logical expressions.
20.
What all gets printed when the following program is compiled and run?
public class test {
public static void main(String args[]) {
int i=0, j=2;
do {
i=++i;
j--;
} while(j>0);
System.out.println(i);
}
}
Correct Answer
C. 2
Explanation
The program will print 2. The do-while loop will execute once because the condition (j > 0) is initially true. Inside the loop, the value of i is incremented by 1 and the value of j is decremented by 1. After the first iteration, the value of i becomes 1 and the value of j becomes 1. Since the condition (j > 0) is false, the loop exits. Finally, the value of i (which is 1) is printed.
21.
Which of these are legal array declarations or definitions?
Correct Answer
A. Int[] []x[];
Explanation
The correct answer is int[] []x[]; This is a legal array declaration because it declares a multidimensional array named x with unspecified dimensions. The int[] [] indicates that x is an array of arrays of integers. The additional [] after x indicates that x itself is also an array.
22.
What gets printed when the following code is compiled and run?
public class test {
public static void main(String args[])
{
int i = 1;
do {
i--;
} while (i > 2);
System.out.println(i);
}
}
Correct Answer
A. 0
Explanation
The code starts with the variable i initialized to 1. It then enters a do-while loop where i is decremented by 1. However, the condition for the loop to continue is that i is greater than 2, which is not the case since i is initially 1. Therefore, the loop is never executed and the value of i remains 1. After the loop, the value of i (which is still 1) is printed, resulting in the output 1.
23.
Assuming the declaration below, which of the following statements would compile?
String s = new String("xyz");
Correct Answer
C. S = s + s;
Explanation
The statement "s = s + s;" would compile because it concatenates two strings together using the "+" operator. The resulting string is then assigned back to the variable "s".
24.
Assume that class A extends class B, which extends class C. Also all the three classes implement the method test(). How can a method in a class A invoke the test() method defined in class C (without creating a new instance of class C)?
Correct Answer
F. It is not possible to invoke test() method defined in C from a method in A.
Explanation
The correct answer is that it is not possible to invoke the test() method defined in class C from a method in class A. This is because the use of "super.super.test()" or "::test()" is not allowed in Java. The "super" keyword can only be used to invoke the immediate superclass's method, not a method from a superclass higher up in the inheritance hierarchy. Therefore, a method in class A cannot directly invoke the test() method defined in class C without creating a new instance of class C.
25.
What gets written on the screen when the following program is compiled and run?
public class test {
public static void main(String args[]) {
int i;
float f = 2.3f;
double d = 2.7;
i = ((int)Math.ceil(f)) * ((int)Math.round(d));
System.out.println(i);
}
}
Correct Answer
E. 9
Explanation
The program declares an integer variable i and assigns it the value of the result of multiplying the ceiling value of the float variable f with the rounded value of the double variable d. The ceiling function rounds up the float value to the nearest integer, while the round function rounds the double value to the nearest integer. Therefore, the value of i is 9.
26.
Name the keyword that makes a variable belong to a class, rather than being defined for each instance of the class.
Correct Answer
A. Static
Explanation
The keyword "static" is used to make a variable belong to a class, rather than being defined for each instance of the class. When a variable is declared as static, it is shared among all instances of the class and can be accessed without creating an object of the class. This allows the variable to have a single value across all instances and can be useful for maintaining common data or for creating utility methods that do not require an instance of the class.
27.
Given the following declarations, which of the assignments given in the options below would compile?
int i = 5;
boolean t = true;
float f = 2.3F;
double d = 2.3;
Correct Answer
C. D = i;
Explanation
The assignment "d = i;" would compile because it is valid to assign an integer value to a double variable. The other assignments would not compile because they involve incompatible types. The assignment "t = (boolean) i;" would not compile because it is not possible to directly cast an integer to a boolean. The assignment "f = d;" would not compile because it is not possible to directly assign a double value to a float variable without a cast. The assignment "f = 2.8;" would not compile because it is not possible to assign a double literal to a float variable without a cast.
28.
Is the following statement true or false. As the toString method is defined in the Object class, System.out.println can be used to print any object.
Correct Answer
A. True
Explanation
The statement is true because the toString method is a default method defined in the Object class, which is the root class for all Java classes. This method is responsible for converting an object to its string representation. Since all classes inherit from the Object class, they also have access to the toString method. Therefore, System.out.println can be used to print any object because it internally calls the toString method to obtain the string representation of the object.
29.
What all gets printed when the following program is compiled and run?
public class test {
public static void main(String args[]) {
int i = 1;
switch(i) {
case 0:
System.out.println(0);
break;
case 1:
System.out.println(1);
case 2:
System.out.println(2);
break;
case 3:
System.out.println(3);
break;
}
}
}
Correct Answer
F. B, c
Explanation
The correct answer is "b, c" because when the program is run, the value of the variable "i" is 1. The switch statement checks the value of "i" and since it matches the case 1, it executes the code block under that case. Therefore, the output "1" is printed. However, there is no break statement after the code block for case 1, so the program continues to execute the code block for case 2 as well. As a result, the output "2" is also printed.
30.
Which of these is a legal definition of a method named m assuming it throws IOException, and returns void? Also assume that the method does not take any arguments.
Correct Answer
A. Void m() throws IOException{}
Explanation
All of the above options are incorrect except for the answer "void m() throws IOException{}". The correct answer is a legal definition of a method named m assuming it throws IOException and returns void. The keyword "void" indicates that the method does not return any value. The method name is "m" and it does not take any arguments. The "throws IOException" statement indicates that the method can throw an IOException, which means it can encounter an input or output error.
31.
How can you ensure that the memory allocated by an object is freed?
Correct Answer
D. Garbage collection cannot be forced. The programmer cannot force the JVM to free the memory used by an object.
Explanation
The correct answer is "Garbage collection cannot be forced. The programmer cannot force the JVM to free the memory used by an object." This is because garbage collection is managed by the JVM and it decides when to free the memory occupied by an object. The programmer can only suggest to the JVM that it is a good time to perform garbage collection by setting all references to the object to new values (null) or by calling the system.gc() method, but it does not guarantee that the memory will be immediately freed.