1)
class GFM11{
public static void main (String[] args) {
int x,y,z;
System.out.println(x+y+z);
}}
What is the result of attempting to compile and run the program?
A. Prints nothing.
B. Prints an undefined value.
C. Prints: 0
D. Compile-time error
Answer: D
Variables declared inside of a block or method are called local variables; they are not automatically initialized. The compiler will generate an error as a result of the attempt to access the local variables before a value has been assigned.
2)
class MWC204 {
public static void main(String[] args) {
int[][] a1 = {{1,2},{3,4,5},{6,7,8,9},{}};
System.out.print(a1.length());
}}
What is the result of attempting to compile and run the program?
A. 4
B. 9
C. None of the above
D. Compile-time error
Answer: D
Length of an array a1 can be find using a1.length.The length() function is used to find the size of a string object.
3)
class EBH015 {
public static void main (String[] args) {
System.out.print((new Object() instanceof Object)+",");
System.out.print((new Object() instanceof String)+",");
System.out.print((new String() instanceof Object));
}}
What is the result of attempting to compile and run the program?
A. Compile-time error
B. Prints: true,true,true
C. Run-time error
D. Prints: true,false,true
Answer: D
The left operand of the instanceof operator must be null or a reference to an instance of an Object or a subclass of Object. The right operand of the instanceof operator must be a class type, interface type or array type. If the left operand is a reference to an instance of the type specified by the right operand or if the left operand is a reference to an instance of a subclass of the type specified by the right operand, then instanceof returns true.
4)
class A {
public static void main(String[] args) {
char a = 'a', b = 'b'; // 'a' = 97, 'b' = 98
System.out.print(a + b + "" + a + b);
}}
What is the result of attempting to compile and run the program?
A. Prints: abab
B. Prints: 195195
C. Prints: ab195
D. Prints: 195ab
Answer: D
Both operands of the first addition operator are promoted from type char to int, and are evaluated as integral numeric values. The right hand operand of the second addition operator is of type String, so the result of the first addition operator is converted to type String, and is concatenated with the right hand operand. As evaluation of the expression continues from left to right, the remaining operands are also converted to type String.
5)
class MWC101 {
public static void main(String[] args) {
int[] a3 = new int[]{1,2}; // 1
int []a4 = {1,2}; // 2
int[] a5 = new int[5]{1,2,3,4,5}; // 3
}}
Compile-time errors are generated at which lines?
A. 1
B. 2
C. 3
D. None of the above
Answer: C
An array creation expression must have either a dimension expression or an initializer. If both are present, then a compile-time error is generated.
6)
What will be the output of the program?
Float f = new Float("12");
switch (f)
{
case 12: System.out.println("Twelve");
case 0: System.out.println("Zero");
default: System.out.println("Default");
}
A. Zero
B. Twelve
C. Default
D. Compilation fails
Answer: D
The switch statement can only be supported by integers or variables more "narrow" than an integer i.e. byte, char, short. Here a Float wrapper object is used and so the compilation fails.
7)
Which one of these lists contains only Java programming language keywords?
A. class, if, void, long, Int, continue
B. goto, instanceof, native, finally, default, throws
C. try, virtual, throw, final, volatile, transient
D. strictfp, constant, super, implements, do
Answer: B
All the words in option B are among the 49 Java keywords. Although goto reserved as a keyword in Java, goto is not used and has no function.
Option A is wrong because the keyword for the primitive int starts with a lowercase i.
Option C is wrong because "virtual" is a keyword in C++, but not Java.
Option D is wrong because "constant" is not a keyword. Constants in Java are marked static and final.
8)
public interface Foo
{
int k = 4; /* Line 3 */
}
Which three piece of codes are equivalent to line 3?
1. final int k = 4;
2. public int k = 4;
3. static int k = 4;
4. abstract int k = 4;
5. volatile int k = 4;
6. protected int k = 4;
A. 1, 2 and 3
B. 2, 3 and 4
C. 3, 4 and 5
D. 4, 5 and 6
Answer: A
(1), (2) and (3) are correct. Interfaces can have constants, which are always implicitly public, static, and final. Interface constant declarations of public, static, and final are optional in any combination.
9)
What is the numerical range of a char?
A. -128 to 127
B. -(215) to (215) - 1
C. 0 to 32767
D. 0 to 65535
Answer: D
A char is really a 16-bit integer behind the scenes, so it supports 216 (from 0 to 65535) values.
10)
public class F0091
{
public void main( String[] args )
{
System.out.println( "Hello" + args[0] );
}
}
What will be the output of the program, if this code is executed with the command line:
> java F0091 world
A. Hello
B. Hello Foo91
C. Hello World
D. The code does not run.
Answer: D
Option D is correct. A runtime error will occur owning to the main method of the code fragment not being declared static:
Exception in thread "main" java.lang.NoSuchMethodError: main
The Java Language Specification clearly states: "The main method must be declared public, static, and void. It must accept a single argument that is an array of strings."
No comments:
Post a Comment