1.
Which will legally declare, construct, and initialize an array?
Correct Answer
D. Int myList [] = {4, 3, 7};
Explanation
The correct answer is "int myList [] = {4, 3, 7}". This statement declares, constructs, and initializes an array named "myList" of type int. The array is initialized with the values 4, 3, and 7. The square brackets [] indicate that it is an array, and the curly braces {} are used to specify the initial values of the array elements.
2.
Which is a valid keyword in java?
Correct Answer
A. Interface
Explanation
In Java, "interface" is a valid keyword. It is used to define a collection of abstract methods, which can be implemented by classes. Interfaces allow for the implementation of multiple inheritance in Java.
3.
What will be the output of the program?public class Foo{ public static void main(String[] args) { try { return; } finally { System.out.println( "Finally" ); } }}
Correct Answer
A. Finally
Explanation
The output of the program will be "Finally". This is because the code inside the finally block will always execute, regardless of whether there is a return statement or an exception thrown in the try block. Therefore, the statement "Finally" will be printed to the console.
4.
What will be the output of the program?public class MyProgram{ public static void main(String args[]) { try { System.out.print("Hello world "); } finally { System.out.println("Finally executing "); } }}
Correct Answer
D. Hello world Finally executing
Explanation
The program will output "Hello world Finally executing". This is because the try block will always execute, and the finally block will also execute regardless of whether an exception is thrown or not. Therefore, the statement "Hello world" will be printed, followed by the statement "Finally executing".
5.
What is the value of "d" after this line of code has been executed?double d = Math.round ( 2.5 + Math.random() );
Correct Answer
B. 3
Explanation
The code snippet is using the Math.round() method to round the result of the expression "2.5 + Math.random()" to the nearest whole number. Since Math.random() generates a random number between 0 (inclusive) and 1 (exclusive), adding it to 2.5 will result in a number between 2.5 and 3.5. Rounding this number will give us 3, so the value of "d" after executing this line of code will be 3.
6.
Public class Myfile{ public static void main (String[] args) { String biz = args[1]; String baz = args[2]; String rip = args[3]; System.out.println("Arg is " + rip); }}
Correct Answer
C. Java Myfile 1 3 2 2
Explanation
The given correct answer is "java Myfile 1 3 2 2". This is because the program is taking command line arguments and storing them in the "args" array. In the given answer, the first argument "1" is stored in the "biz" variable, the second argument "3" is stored in the "baz" variable, and the third argument "2" is stored in the "rip" variable. Finally, the program prints "Arg is 2" to the console.
7.
Which one is a valid declaration of a boolean?
Correct Answer
C. Boolean b3 = false;
Explanation
The correct answer is "boolean b3 = false;". This is a valid declaration of a boolean because it assigns the value "false" to the variable "b3". In Java, boolean variables can only have two possible values: "true" or "false".
8.
Which is a valid declarations of a String?
Correct Answer
A. String s1 = null;
Explanation
The correct answer is "String s1 = null;". This is a valid declaration of a String because it assigns a null value to the variable "s1". The other options are not valid declarations of a String. "String s2 = 'null';" is incorrect because it uses single quotes instead of double quotes to denote a String literal. "String s3 = (String) 'abc';" is incorrect because it tries to cast a character literal to a String, which is not allowed. "String s4 = (String) '\ufeed';" is incorrect because it tries to cast a character literal to a String, but the character literal is not a valid Unicode escape sequence.
9.
Public interface Foo{ int k = 4; /* Line 3 */}Which three piece of codes are equivalent to line 3?- final int k = 4;
- public int k = 4;
- static int k = 4;
- abstract int k = 4;
- volatile int k = 4;
- protected int k = 4;
Correct Answer
A. 1, 2 and 3
Explanation
The correct answer is 1, 2 and 3 because all three options declare the variable "k" with the value of 4. Option 1 declares "k" as a final variable, option 2 declares "k" as a public variable, and option 3 declares "k" as a static variable. These options are equivalent to line 3 in the given code.
10.
Public class Test { }What is the prototype of the default constructor?
Correct Answer
C. Public Test( )
Explanation
The correct answer is "public Test( )". In Java, the prototype of a default constructor is the same as the class name followed by an empty set of parentheses. In this case, the class name is "Test" and the constructor has no parameters, so the correct prototype is "public Test( )".