Sun Certified
Programmer for Java 2 Platform 1.4 (CX-310-035) Practice Questions
Q. No
Question
11
Which of the following are legal identifiers ?
A
1variable
B
variable1
C
$anothervar
D
_anothervar
E
%anothervar
B
, C and D
A variable name
should start with a
letter, under score
or a dollar sign. A
valid variable can
contain letters and
digits.
12
What will happen when you compile the following
code
public class MyClass{
static int i;
public static void main (String argv[])
{
System.out.println(i) ;
}
}
A
0
B
1
C
Compiler Error variable 'i'
may not have initialized.
D
null
A
All the class level
variables are
automatically
initialized to a
default value. Since
'i' is of type int,
it is initialized to
zero.
13
What will be the output on executing the
following code.
public class MyClass {
public static void main (String args[] ) {
int abc[] = new int [] {1, 2, 3, 4};
System.out.println(abc[2]);
}
}
A
ArrayIndexOutofBoudsException will be thrown.
B
2
C
3
D
4
C
Always array
elements starts with
index zero. The
element at index 2,
ie, the e3lement at
position 3 is 3 . So
the correct answer
is C.
14
What will be the output on executing the
following code.
public class MyClass {
public static void main (String args[] ) {
int abc[] = new int [5];
System.out.println(abc);
}
}
A
Error array not initialized
B
5
C
null
D
Print some junk characters
D
It will
print some junk
characters to the
output. Here it will
not give any compile
time or runtime
error because we
have declared and
initialized the
array properly.
Event if we are not
assigning a value to
the array, it will
always initialized
to its defaults.
15
What will be the output on executing the following
code.
public class MyClass {
public static void main (String args[] ) {
int abc[] = new int [5];
System.out.println(abc[0]);
}
}
A
Error array not initialized
B
5
C
0
D
Print some junk characters
C
Here it will not
give any compile
time or runtime
error because we
have declared and
initialized the
array properly.
Event if we are not
assigning a value to
the array, it will
always initialized
to its defaults. So
the array will be
initialized with
values zero.
16
What will be the result of attempting to
compile and run the following code ?
abstract class MineBase {
abstract void amethod() ;
static int i;
}
public class Mine extends MineBase {
public static void main( String
argv[]) {
int[] ar=new int[5];
for (i=0;i < ar.length;i++)
System.out.println (ar[i]) ;
}
}
A
A sequence of 5 0's
will be printed
B
Error: variable 'ar' may not
have initialized .
C
Error Mine must be declared
abstract
D
Error MineBase cannot be
declared abstract.
C
If we are extending
an abstract class,
we need to provide
implementations for
every abstract
method. If we are
not able to provide
the implementation,
we have to declare
our child class as
abstract. Otherwise
the compiler will
flag an error
message.
17
What will be printed out if you attempt to
compile and run the following code ?
int i=1;
switch (i) {
case 0:
System.out.println ("zero") ;
break;
case 1:
System.out.println("one") ;
break;
case 2:
System.out.println("two") ;
break;
default:
System.out.println("default") ;
}
A
one
B
zero
C
one default
D
one two default
A
It is obvious. It
printed the value
corresponding for
the case label 1.
18
What will be printed out if you attempt to compile
and run the following code ?
int i=1;
switch (i) {
case 0:
System.out.println ("zero") ;
break;
case 1:
System.out.println("one") ;
case 2:
System.out.println("two") ;
break;
}
A
zero
B
one
C
one two
D
Error no default specified.
C
Choice C is
the correct because,
since the value of i
is one, it will
start executing the
case 1. But it will
continue its
execution till it
finds a break or
till it reach the
end of switch
statement, so the
value one followed
by two will be
printed. Choice D is
incorrect because,
the use of default
in switch expression
is optional.
19
What will be printed out if you attempt to
compile and run the following code ?
int i=10;
switch (i) {
default:
System.out.println("Default");
case 0:
System.out.println ("zero") ;
break;
case 1:
System.out.println("one") ;
case 2:
System.out.println("two") ;
break;
}
A
default
B
default zero
C
Error , default cannot be
the first statement in switch.
D
default zero one
two
B
Since there is no
matching case
labels, it will
start its execution
in default, and
continue till the
first break
statement. The order
of case labels in
switch statement
does not matter.
20
Which of the following data types can appear
inside a switch statement as its label ?
A
String
B
char
C
int
D
byte
B
, C and D
The valid types for
the switch statement
is byte, char, short
and int.