1. 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());
(C) and (D)
(B) and (D)
(A), (B), (C) and (D)
(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.
2. 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
Class MyDisplay must implement the abstract method show(), or it must also be declared abstract.
3. Consider the following code:
class NewString extends java.lang.String
Results in error because class body is not defined
Results in error because String isabstract
Results in error because the class is not declaredpublic
Results in error because java.lang.String isfinal
Compiles successfully
java.lang.String is a final class and cannot be extended.
4. Consider the following code:
int i = 1;
switch(c)
code is illegal and therefore will not compile
output will be One
output will be One followed by Two
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.
5Are 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
The method func1, which is declared as abstract cannot have a body as in the code above.
6.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), (B) & (D)
(A)
(A) & (C)
(A), (C) & (D)
Integers cannot be converted to booleans, and floats must be explicitly specified (22.3f, and not 22.3 which is a double).
7.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)
(A) & (D)
(B)
(A) & (C)
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.
8.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 3 and Line 4
Line 1 and Line 4
Line 4 only
Line 3 only
Line 1 only
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.
9.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");
None of these
"x is negative"
"x is positive"
"x is positive" and "x is negative"
The "else" statement corresponds to the second "if" in this case. To have it correspond to the first "if", curly braces
10.The control expression in an "if" statement must be:
an expression with either the type boolean or integer
an expression with type integer
an expression with type boolean
an expression with either the type boolean or integer with value 0 or 1
In the Java programming language, only expressions of type boolean are allowed as control expressions in an "if" statement.
1) What is the value of a?
2) int a = 7;
int b = 4;
a = b;
a = a + 1;
One answer only.
4
5
7
8
________________________________________
2) What output will the following line produce? System.out.println("The answer is: "+17+3);
One answer only.
The answer is: 20
The answer is: 17+3
The answer is: 173
The answer is:
________________________________________
3) What is the value of x?
4) int x = 8 / 3;
One answer only.
1
2
2.666666667
3
________________________________________
4) What is the value of y?
int y = 19 % 4;
One answer only.
2
3
4
4.75
________________________________________
5) What is the value of z?
double z = 7 / (3 + 0.5);
One answer only.
0.5
2.0
2.833333333
3.0
Question 1:
What is the value of a?
int a = 7;
int b = 4;
a = b;
a = a + 1;
Options: Your Response: Correct Answer:
4
5
7
8
see a demonstration.
________________________________________
Question 2:
What output will the following line produce?
System.out.println("The answer is: "+17+3);
Options: Your Response: Correct Answer:
The answer is: 20
The answer is: 17+3
The answer is: 173
The answer is:
see a demonstration.
________________________________________
Question 3:
What is the value of x?
int x = 8 / 3;
Options: Your Response: Correct Answer:
1
2
2.666666667
3
see a demonstration.
________________________________________
Question 4:
What is the value of y?
int y = 19 % 4;
Options: Your Response: Correct Answer:
2
3
4
4.75
see a demonstration.
________________________________________
Question 5:
What is the value of z?
double z = 7 / (3 + 0.5);
Options: Your Response: Correct Answer:
0.5
2.0
2.833333333
3.0
see a demonstration.
Wednesday, July 8, 2009
Subscribe to:
Posts (Atom)