Question 251–d
What is the output of running A?
public class A extends B {
int x = 1;
public static void main(String[] args) {
System.out.print(new A().x);
System.out.print(new B().x);
}
}
class B {
int x = 5;
}
-
51
-
11
-
55
-
15
Question 252–a
What is the output of running class C?
class A {
public A() {
System.out.println(
"The default constructor of A is invoked");
}
}
class B extends A {
public B() {
System.out.println(
"The default constructor of B is invoked");
}
}
public class C {
public static void main(String[] args) {
B b = new B();
}
}
a) "The default constructor of A is invoked"
"The default constructor of B is invoked"
"The default constructor of B is invoked"
b) none
c) "The default constructor of A is invoked"
"The default constructor of B is invoked"
"The default constructor of A is invoked"
Question 253–d
What is the output of running class Test.
public class Test {
public static void main(String[] args) {
new Circle9();
}
}
public abstract class GeometricObject {
protected GeometricObject() {
System.out.print("A");
}
protected GeometricObject(String color, boolean filled) {
System.out.print("B");
}
}
public class Circle9 extends GeometricObject {
/** Default constructor */
public Circle9() {
this(1.0);
System.out.print("C");
}
/** Construct circle with a specified radius */
public Circle9(double radius) {
this(radius, "white", false);
System.out.print("D");
}
/** Construct a circle with specified radius, filled, and color */
public Circle9(double radius, String color, boolean filled) {
super(color, filled);
System.out.print("E");
}
}
-
AEDC
-
CBAE
-
ABCD
-
BEDC
-
BACD
Question 254–c
What is the output of the following code?
1 int i = 45678;
2 int j = ~i;
3
4 System.out.println(j);
-
Compilation error at line 2. ~ operator applicable to boolean values only
-
Prints 45677
-
Prints –45679
-
Prints -45677
Question 255–c
What is the output of the following code?
class X {
public static void main (String x [ ]) {
B myB = new B( );
if (myB instanceof C)
System.out.println ("TRUE");
else
System.out.println ("FALSE");
}//main
}//class
-
FALSE
-
TRUE FALSE
-
doesn't compile
-
no output to screen
-
TRUE
Question 256–c
What is the output of the following code?
int m;
for (int j = 2, m = 1; j > 0; j--) {
for (int k = 2; ; k--) {
if (k == 1) break;
System.out.println (j+" "+m+" "+k);
}
}
-
212, 211, 112, 111
-
211, 112, 111
-
none of the other answers (or code doesn't compile)
-
212, 211, 210, 112, 111, 110
-
212, 112
Question 257–d
What is the output of the following code:
public class Test {
public static void main(String[] args) {
Object o1 = new Object();
Object o2 = new Object();
System.out.print((o1 == o2) + " " + (o1.equals(o2)));
}
}
-
true true
-
true false
-
false true
-
false false
Question 258–a
What is the output of the following code:
public class Test {
public static void main(String[] args) {
String s1 = new String("Java");
String s2 = new String("Java");
System.out.print((s1 == s2) + " " + (s1.equals(s2)));
}
}
-
false true
-
true true
-
false false
-
true false
Question 259–a
What is the output of the following code?
public class Test {
public static void main(String[] args) {
String s1 = new String("Welcome to Java");
String s2 = s1;
s1 += "and Welcome to HTML";
if (s1 == s2)
System.out.println("s1 and s2 reference to the same String object");
else
System.out.println("s1 and s2 reference to different String objects");
}
}
-
s1 and s2 reference to different String objects
-
s1 and s2 reference to the same String object
Question 260–a
What is the output of the following code?
public class Test {
public static void main(String[] args) {
String s1 = new String("Welcome to Java!");
String s2 = s1.toUpperCase();
if (s1 == s2)
System.out.println("s1 and s2 reference to the same String object");
else if (s1.equals(s2))
System.out.println("s1 and s2 have the same contents");
else
System.out.println("s1 and s2 have different contents");
}
}
-
s1 and s2 have different contents
-
s1 and s2 reference to the same String object B. s1 and s2 have the same contents
Question 261–d
What is the output of the following code segment?
int num = 4;
int power = 3;
int product = 0;
int i = 0;
for (i = 0; i < power; i++)
product *= num;
System.out.println(product);
-
64
-
16
-
256
-
0
Question 262–a
What is the output of the following code snippet?
public static void main (String x[ ]) {
int a = 4;
a ^= 2;
System.out.println (a);
}
-
The code prints out 6
-
The code doesn't compile because there is no shortcut expression ^=
-
The code prints out 0
-
The code doesn't compile because you cannot print a variable that has been ^'d
-
The code prints out 4
Question 263–a
What is the output of the following program?
33. import java.util.*;
34. class Question {
35. public static void main(String args[]) {
36. HashSet set = new HashSet();
37. String s1 = "abc";
38. String s2 = "def";
39. String s3 = "";
40. set.add(s1);
41. set.add(s2);
42. set.add(s1);
43. set.add(s2);
44. Iterator i = set.iterator();
45. while(i.hasNext()){
46. s3 += (String) i.next();
47. }
48. System.out.println(s3);
49. }
}
-
defabc
-
fedcbafedcba
-
abcdefabcdef
-
defabcdefabc
Question 264–b
What is the output of the following program?
import java.util.*;
public class Question {
public static void main(String args[]){
String strings[] = {"skis","bobsled",
"figure skates"};
List list = Arrays.asList(strings);
displayList(list);
}
static void displayList(List list) {
ListIterator i = list.listIterator(0);
while(i.hasNext()){
Object o = i.next();
if(o == null) System.out.println("null");
else System.out.println(o.toString());
}
}
}
a) bobsled
figure skates
skis
b) skis
bobsled
figure skates
c) null
d) A compiler error
Question 265–a
What is the output of the following program?
public class Test {
public static void main(String[] args) {
new A();
}
{
System.out.print("Z");
}
}
class A extends B {
A() {
System.out.print("A");
}
{
System.out.print("X");
}
}
class B {
B() {
System.out.print("B");
}
{
System.out.print("Y");
}
}
-
YBXA
-
BXAY
-
XYAB
-
YBXAZ
Question 266–d
What is the output of the following program?
public class Test extends Object {
public static void main(String[] args) {
Test test = new Test();
}
public Test() {
System.out.print("A");
}
{
System.out.print("B");
}
static {
System.out.print("C");
}
}
-
BAC
-
ABC
-
ACB
-
CBA
-
CAB
Question 267–c
What is the output of the following program when it is invoked using the command line java Test: what do you say?
class Test {
public static void main(String[] args) {
System.out.println(args[2]);
}
}
-
what
-
say
-
you
-
do
Question 268–d
What is the path from node C to I?
-
C, H, G, I
-
C, G, E, I
-
C, B, H, I
-
C, E, H, I
-
C, D,E, I
Question 269–b
What is the printout for the first statement in the main method?
public class Foo {
static int i = 0;
static int j = 0;
public static void main(String[] args) {
int i = 2;
int k = 3;
{
int j = 3;
System.out.println("i + j is " + i + j);
}
k = i + j;
System.out.println("k is " + k);
System.out.println("j is " + j);
}
}
-
i + j is 22
-
i + j is 23
-
i + j is 6
-
i + j is 5
Question 270–a
What is the printout of the second println statement in the main method?
public class Foo {
int i;
static int s;
public static void main(String[] args) {
Foo f1 = new Foo();
System.out.println("f1.i is " + f1.i + " f1.s is " + f1.s);
Foo f2 = new Foo();
System.out.println("f2.i is " + f2.i + " f2.s is " + f2.s);
Foo f3 = new Foo();
System.out.println("f3.i is " + f3.i + " f3.s is " + f3.s);
}
public Foo() {
i++;
s++;
}
}
-
f2.i is 1 f2.s is 2
-
f2.i is 2 f2.s is 1
-
f2.i is 2 f2.s is 2
-
f2.i is 1 f2.s is 1
Question 271–d
What is the printout for the third statement in the main method?
public class Foo {
static int i = 0;
static int j = 0;
public static void main(String[] args) {
int i = 2;
int k = 3;
{
int j = 3;
System.out.println("i + j is " + i + j);
}
k = i + j;
System.out.println("k is " + k);
System.out.println("j is " + j);
}
}
-
j is 1
-
j is 3
-
j is 2
-
j is 0
Question 272–c
What is the purpose of count < phrase.length in the following code?
int[] letterCount = new int[26];
for (int count = 0; count < phrase.length; count++) {
String current = phrase[count];
char[] letters = current.toCharArray();
}
-
sets the count variable to the number of letters in a phrase
-
determines the number of letters in a phrase
-
makes the loop stop when the end of the phrase array is reached
Question 273–c
What is the purpose of the getSeconds() method in this class?
public class Virus {
private int newSeconds = 86;
public int getSeconds() {
return newSeconds;
}
-
it changes the value of newSeconds
-
there is not enough code to tell what its purpose is
-
it makes the value newSeconds available to other objects
Question 274–b
What is the result of compiling and running the following program.
Select the one correct answer.
class test {
public static void main(String args[]) {
char ch;
String test2 = "abcd";
String test = new String("abcd");
if(test.equals(test2)) {
if(test == test2)
ch = test.charAt(0);
else
ch = test.charAt(1);
}
else {
if(test == test2)
ch = test.charAt(2);
else
ch = test.charAt(3);
}
System.out.println(ch);
}
}
-
'c'
-
'b'
-
'a'
-
'd'
Question 275–e
What is the result of executing the following Java class:
import java.awt.*;
public class FrameTest extends Frame {
public FrameTest() {
add (new Button("First"));
add (new Button("Second"));
add (new Button("Third"));
pack();
setVisible(true);
}
public static void main(String args []) {
new FrameTest();
}
}
Select from the following options:
-
Nothing happens.
-
A runtime exception is generated (no layout manager specified).
-
Three buttons are displayed across a window.
-
Only the "second" button is displayed.
-
Only the "third" button is displayed.
-
Only the "first" button is displayed.
Question 276–b
What is the result of the following code?
class X {
int myVar;
X ( ) {
System.out.println (myVar);
}
}//--------------------------------------
class Driver {
public static void main(String x[]) {
X myX= new X();
}
}//---------------------------------------
-
the code compiles but will the give a Runtime Exception
-
the code compiles and prints out 0
-
The code doesn't compile because the attribute myVar was never initialized
-
The code doesn't compile because print statements aren't allowed in the constructor
Question 277–a
What is the results of the code below?
byte b = 6;
byte c = 201;
System.out.println ("B + C =" + (b+c));
-
The code does not compile
-
The code prints out "B + C =-41"
-
The code prints out "B + C =207"
Question 278–c
What is the result of the following program?
class Question {
public static void main(String[] args) {
int i = 7;
int j = 8;
int n = (i | j) % (i j);
System.out.print(n);
}
}
-
15
-
0
-
An ArithmeticException is thrown.
-
-15
Question 279–b
What is the value displayed by the following program?
class Question {
public static void main(String[] args) {
int x=0;
boolean b1, b2, b3, b4;
b1=b2=b3=b4=true;
x=(b1 | b2 b3 ^ b4) ? x++ : --x;
System.out.println(x);
}
}
-
2
-
0
-
–1
-
–2
-
3
-
1
Question 280–d
What is the value of data after execution of the following code?
JTextField [] data = new JTextField[10];
for(int i=0; i
data[i] = new JTextField(20);
}
data = null
data[i] = null
data[i] stores a reference to an array object
data[i] stores a reference to a JTextField
Question 281–
What is the value of myCount.count displayed?
public class Test {
public static void main(String[] args) {
Count myCount = new Count();
int times = 0;
for (int i=0; i<100; i++)
increment(myCount, times);
System.out.println(
"myCount.count = " + myCount.count);
System.out.println("times = "+ times);
}
public static void increment(Count c, int times) {
c.count++;
times++;
}
}
class Count {
int count;
Count(int c) {
count = c;
}
Count() {
count = 1;
}
}
-
98
-
101
-
100
-
99
Question 282–d
What is the value of data after execution of the following code?
JTextField [] data = new JTextField[10];
for(int i=0; i
data[i] = new JTextField(20);
}
data[i] stores a reference to an array object
data[i] = null
data = null
data[i] stores a reference to a JTextField
Question 283–c
What is the value of the variable age when it is displayed in the following method?
public static void main(String args[])
{
int age=30;
float cost = age*1.5;
System.out.println("Age is "+age+". Cost is "+cost);
}
-
45.0
-
30.0
-
30
-
45
Question 284–d
What is the value of times displayed?
public class Test {
public static void main(String[] args) {
Count myCount = new Count();
int times = 0;
for (int i=0; i<100; i++)
increment(myCount, times);
System.out.println(
"myCount.count = " + myCount.count);
System.out.println("times = "+ times);
}
public static void increment(Count c, int times) {
c.count++;
times++;
}
}
class Count {
int count;
Count(int c) {
count = c;
}
Count() {
count = 1;
}
}
-
100
-
101
-
98
-
0
Question 285–a
What is the value of y after execution of the following statements?
int x = 5;
int y = 4;
y = x++;
-
5
-
6
-
7
-
4
Question 286–d
What is written to the standard output as the result of executing the following statements?
Boolean bl = new Boolean(true);
Boolean b2 = new Boolean(true);
Object obj 1 = (Obj ect) b1;
Object obj2 = (Object) b2;
if(objl ==obj2)
if (obj 1.equals(obj2))
System.out.println("a");
else
System.out.println("b");
else
if (obj 1 .equals(obj2))
System.out.println("c");
else
System.out.println("d");
Select the one right answer.
-
b
-
a
-
d
-
c
Question 287–a
What is written to the standard output given the following statement:
System.out.println(4 7);
-
4
-
7
-
5
-
0
-
6
Question 288–bc
What is wrong with the following code?
final class First {
private int a = 1;
intb=2;
}
class Second extends First {
public void method() {
System.out.println(a + b);
}
}
-
You cannot invoke println() without passing it a String
-
Second cannot extend First
-
Since a is private, no classes other than First can access it
-
final is not a valid keyword for a class
Question 289–c
What is wrong with the following code segment?
int [] data = new int[9];
for(int i = 0; i<=9; i+=1){
data[i] = i;
}
-
nothing wrong
-
data[i] = i; should be data = i;
-
i<=9 should be i<9
-
int [] data = new int[9] should be int [] data;
Question 290–d
What keyword can you use to refer to the methods and variables of a superclass?
-
this
-
call
-
get
-
super
Question 291–c
What modifier should you use on a class so that a class in the same package can access it but a class in a different package cannot access it?
-
Protected
-
Private
-
Use the default modifier.
-
public
Question 292–a
What modifier should you use on the members of a class so that they are not accessible to another class in a different package, but are accessible to any subclasses in any package?
-
protected
-
public
-
Use the default modifie
-
private
Question 293–ac
What must be true for the RuriHandler class so that instances of RunHandler can be used as written in the code below:
class Test {
public static void main(String[] args) {
Thread t = new Thread(new RunHandler());
t.start();
}
}
-
RunHandler must provide a run() method declared as public and returning void.
-
RunHandler must extend the Thread class.
-
RunHandler must implement the java.lang.Runnable interface.
-
RunHandler must provide an init() method.
Question 294–bc
What order can you place the following pieces of a source file in so that the source file will compile without errors or warnings?
//A
import j ava.applet. *;
//B
class Helper {
}
//C
package myclasses;
//D
public class MyApplet extends java.applet.Applet {
}
-
A, B, C, D
-
C, A, B, D
-
C, A, D, B
-
A, C, B, D
-
C, B, A, D
Question 295–b
What output does the following program display?
14. import java.util.*;
15. class Question {
16. public static void main(String args[]) {
17. String s1 = "abc";
18. String s2 = "def";
19. Stack stack = new Stack();
20. stack.push(s1);
21. stack.push(s2);
22. try {
23. String s3 = (String) stack.pop() + (String) stack.pop();
24. System.out.println(s3);
25. }catch(EmptyStackException ex){
26. }
27. }
}
-
abcabc
-
defabc
-
abcdef
-
defdef
9>
Chia sẻ với bạn bè của bạn: |