Trắc nghiệm Core Java
1. Which of the following statements are valid, given the following variable declarations:
boolean a; boolean b; int c; (Select a valid answer)
A
(a && c)
C
(a ^ b) | c
B
(a || a)
D
(a & c)
Answer: B Mark: 1.0
2. Which of the following can be applied to constructors: (Select a valid answer)
A
final
B
static
C
synchronized
D
None of these.
Answer: D Mark: 1.0
3. Which of the following retain their preferred size (width and height) when added
(individually) to the North section of a container with a BorderLayout (assume that no
other components or containers are present in the North section).
(Select a valid answer)
A
TextArea
B
Button
C
TextField
D
Checkbox
Answer: D Mark: 1.0
4. Which of the following are legal names for variables. (Select a valid answer)
A
int
B
%large
C
Integer
D
2much
Answer: C Mark: 1.0
5. Consider the following piece of code and select the correct statement from the following.
String s = new String("abcdefgh"); // (1)
s.replace('d', 'q'); // (2)
System.out.println(s); // (3)
(Select the one right answer)
A
The code fails to compile, reporting
an error at line 2. Strings are immutable,
and therefore a replace() method is
meaningless.
B
The code compiles correctly, and
displays the text "abcqefgh".
C
The code compiles correctly, and
displays the text "abcdefgh".
D
The code compiles, but an exception
is thrown when line 2 is executed.
Answer: C Mark: 1.0
6. Which of the following keywords can be applied to the variables or methods of an
interface. (Select all valid answers)
A
static
B
private
C
public
D
protected
Answer: A, C Mark: 0.5
7. Consider the following piece of code and select the correct statement(s):
1. class A{
2. protected int method(){
3. }
4. }
5.
6. class B extends A{
7. int method(){
8. }
9. }
(Select all valid answers)
A
The code fails to compile, because
you can't override a method to be more
private than its parent.
B
The code fails to compile, because
method() is declared as protected, and is
therefore not available to any subclass.
C
The code fails to compile. However, it
can be made to compile correctly by
prefixing line 7 with the access qualifier
"protected".
D
The code fails to compile. However, it
can be made to compile correctly by
prefixing line 7 with the access qualifier
"public".
Answer: A, C , D Mark: 1.0
8. Consider the following piece of code (assume the Graphics context g is defined
correctly):
g.setBackground(Color.red);
g.setForeground(Color.white);
g.drawLine(10, 10, 50, 10);
g.setForeground(Color.blue);
g.drawRect(100, 100, 50, 50);
What is displayed when this code is executed. (Select a valid answer)
A
A blue line from (10,10) to (50,10)
and a blue rectangle with upper left
corner at (100,100).
B
A white line from (10,10) to (50,10)
and a blue square with top left corner at
(100,100).
C
A white line from (10,10) to (10,50)
and a blue square with lower left corner
at (100,100).
D
A red line from (10, 10) to (50,10)
and a red square with upper left corner at
(100,100).
Answer: B Mark: 1.0
9. Consider the following piece of code.
class Test{
public static void main(String [] args){
System.out.println(args[3]);
}
}
When the following is typed at the command line, what is displayed: (Select a valid
answer)
java Test Metallica Justice For All
A
All
B
For
C
Justice
D
Nothing
Answer: A Mark: 1.0
10. Consider the following piece of code.
1. String s = "abcd";
2. Integer x = new Integer(3);
3. String s2 = s + 4;
4. s2 = null;
5. s = null;
Following the execution of which line above, is the object referenced by s2 available for
garbage collection. (Select the one right answer)
A
Line 5
B
It is not possible to say when an object
is available for garbage collection.
C
Line 4
D
The objects are not available until the
executing thread is ended.
Answer: C Mark: 1.0
11. What is displayed when the following piece of code is compiled and executed:
class Test{
public static void main(String [] args){
Base b = new Subclass();
System.out.println(b.x);
System.out.println(b.method());
}
}
class Base{
int x = 2;
int method(){
return x;
}
}
class Subclass extends Base{
int x = 3;
int method(){
return x;
}
}
(Select the one right answer)
A
Nothing. The code fails to compile
because the object b is not created in a
valid way.
B
2
3
C
2
D
3
2 3
Answer: B Mark: 1.0
12. What is displayed when the following is executed:
String s1 = "aaa";
s1.concat("bbb");
System.out.println(s1);
(Select the one right answer)
A
The string "aaa".
B
The string "aaabbb".
E
The string "bbb".
D
The string "bbbaaa".
Answer: A Mark: 0.5
13. What is the output of the following piece of code:
1. int x = 6;
2. double d = 7.7;
3.
4. System.out.println((x > d) ? 99.9 : 9);
(Select the one right answer)
A
9
B
9.0
C
99.9
D
Nothing. An ArithmeticException is
thrown at line 4.
Answer: B Mark: 1.0
14. Which of the following applet tags are mandatory (select all the correct answers).
A
CODE
B
WIDTH
C
HEIGHT
D
PARAM
Answer: A, B, C Mark: 1.0
15. What is printed out following the execution of the code below:
1. class Test{
2. static String s;
3. public static void main(String []args){
4. int x = 4;
5. if (x < 4)
6. System.out.println("Val = " + x);
7. else
8. System.out.println(s);
9. }
10. }
(Select the one right answer)
A
Nothing. The code fails to compile
because the String s isn't declared
correctly.
B
The text "Val = null" is displayed.
C
The string "Val = " is displayed.
D
The text "null" is displayed
Answer: D Mark: 1.0
16. What is displayed when the following piece of code is executed (assume the graphics
context, g, is correctly set up):
g.drawString("abc", 10, 10);
(Select the one right answer)
A
The text "abc" with the lower left part
of "a" located at x = 10, y = 10.
B
The text "abc" with the upper left part
of "a" located at x = 10, y = 10.
C
Nothing. This is not a valid method.
D
The text "abc" with the lower right
part of "a" located at x = 10, y = 10.
Answer: A Mark: 0.5
17. To reference a JAR from a web page, which of the following keywords are used:
(Select the one right answer)
A
jar
C
zip
B
class
D
archive
Answer: D Mark: 0.5
18. Analyse the following 2 classes and select the correct statements.
class A{
private int x = 0;
static int y = 1;
protected int q = 2;
}
class B extends A{
void method(){
System.out.println(x);
System.out.println(y);
System.out.println(q);
}
}
(Select all valid answers)
A
The code fails to compile because the
variable x is not available to class B.
B
The code compiles correctly, and the
following is displayed: 0 1 2
C
The compiler will complain that the
variable x in class B is undefined.
D
Removing the line
"System.out.println(x)" will allow the
code to compile correctly.
Answer: A, C, D Mark: 1.0
19. Which of the following interfaces can be used to manage a collection of elements, with
no duplication.
(Select the one right answer)
A
List
B
Vector
C
Set
D
None of them
Answer: C Mark: 0.5
20. Which of the following are valid ways to define an abstract method.
(Select all valid answers)
A abstract void Test();
B abstract void Test() {}
C static abstract void Test();
D final abstract void Test();
E Methods cannot be defined as abstract, only variables can be abstract.
A
abstract void Test();
B
Methods cannot be defined as abstract,
only variables can be abstract.
C
abstract static void Test();
D
final abstract void Test();
Answer: A Mark: 0.5