Question | Answer | Explanation |
Which of the following expressions will produce errors upon compilation? (A) boolean a = (boolean) 1; (B) boolean b = (false && true); (C) float y = 22.3; (A)
(A), (C) & (D)
(A) & (C) (A), (B) & (D)
| (A) & (C) | Integers cannot be converted to booleans, and floats must be explicitly specified (22.3f, and not 22.3 which is a double). |
Which lines of the following will produce an error? 1. byte a1 = 2, a2 = 4, a3; 2. short s = 16; 3. a2 = s; 4. a3 = a1 * a2; (The lines are numbered only for illustration in this question.) Line 1 only
Line 3 and Line 4 Line 3 only
Line 4 only
Line 1 and Line 4
| Line 3 and Line 4 | In line 3, a short value is being assigned to a variable of type byte, which is illegal. Also, the "*" operator promotes the bytes to integers, which makes it illegal to assign the return value to a variable of type byte. |
Examine the following code snippets to identify the legal loop constructs: (A) for (int i = 22, int j = 0; i + j > 11; i = i-3, j++)
(A) & (D) (A)
(B)
(A) & (C)
| (A) & (D) | In option (B), the argument of "while" is an integer, which is illegal. It can only be boolean. In option (C), "int i > 0" is an illegal construct. |
Determine the output when the value of x is zero: if(x >= 0) if(x > 0) System.out.println("x is positive"); else System.out.println("x is negative");
"x is negative" "x is positive"
"x is positive" and "x is negative"
None of these
| "x is negative" | The "else" statement corresponds to the second "if" in this case. To have it correspond to the first "if", curly braces |
The control expression in an "if" statement must be: an expression with type integer
an expression with either the type boolean or integer
an expression with type boolean an expression with either the type boolean or integer with value 0 or 1
| an expression with type boolean | In the Java programming language, only expressions of type boolean are allowed as control expressions in an "if" statement. |
To print the value of a variable "x" of type int, which of the following expressions can be used: (A) System.out.println("x = " + x); (B) System.out.println("x = " + String.valueOf(x)); (C) System.out.println("x = " + Integer.toString(x)); (D) System.out.println("x = " + (new Integer(x)).toString());
(B), (C) and (D)
(A), (B), (C) and (D) (B) and (D)
(C) and (D)
| (A), (B), (C) and (D) | Other than the functions in the String and Integer class, the + operator can be used directly with one String operand and other one int. |
Consider the following code: int i = 1; switch(c)
output will be One, followed by Two, and then followed by Three output will be One followed by Two
output will be One
code is illegal and therefore will not compile
| output will be One, followed by Two, and then followed by Three | The flow of control in switch statements goes subsequently through the case statements unless explicitly broken using the break statement. In which case, the flow transfers to the first statement after the switch block. Without any break statements, all the cases are executed. |
Consider the following code: class NewString extends java.lang.String
Results in error because class body is not defined
Results in error because the class is not declaredpublic
Results in error because java.lang.String isfinal Results in error because String isabstract
Compiles successfully
| Results in error because java.lang.String isfinal | java.lang.String is a final class and cannot be extended. |
Examine the following class definitions to detect errors, if any. abstract class MyPanel
1 Error. Method show() should have a return type
1 Error. Method show() is not implemented in MyDisplay 1 Error. MyDisplay does not contain any members
No errors
| 1 Error. Method show() is not implemented in MyDisplay | Class MyDisplay must implement the abstract method show(), or it must also be declared abstract. |
Are there any errors in the following class definition? abstract class Class1
Class header definition is wrong
Method definition is wrong Constructor needs to be defined
No errors
| Method definition is wrong | The method func1, which is declared as abstract cannot have a body as in the code above. |
For the following class definition, which is a legal statement to construct an object of type Class1: class Class1 extends Class2
(A)
(C) & (E)
(A) & (B)
(C) & (D) None of the above
| (C) & (D) | The two constructors defined for Class1 are used in options (C) and (D). All other constructors are invalid. |
The method int func(int i, int j) (B) & (C)
(C) & (D)
(A), (B), (C) & (E)
(A), (B) & (E) None of these
| (A), (B) & (E) | Method overloading can be done by specifying different arguments (or types). Only having a different return type causes compilation error (as in C above). |
Which error does the following code contain: class Class1
There is no error
Method func1() must be declared as static
Class1 should be declared as abstract Class Class1 has not been declared public
| Class1 should be declared as abstract | A class containing an abstract method must also be declared abstract. |
What will be the output of the following program? class Main1
XXX
YYY
XXX followed by YYY
Error. Won't compile
| Error. Won't compile | The statement System.out.prinln("YYY"); is not reachable. |
What will be the output of the following program? class Main2
XXX followed by YYY XXX
YYY
Error. Won't compile
| XXX followed by YYY | |
What is java_g used for? Using the jdb tool
Executing a class with optimization turned off To provide information about deprecated methods
None of these
| Executing a class with optimization turned off | |
With javadoc, which of the following denotes a javadoc comment? //#
/** /*
//**
| /** | |
Which of the following command lines options generates documentation for all classes and methods? -protected
-private -public
-verbose
-encoding
| -private | |
Which javadoc tag is used to denote a comment for a method parameter? @method
@parameter
@argument
@value
@param
| @param | |