Sun Certified
Programmer for Java 2 Platform 1.4 (CX-310-035) Practice Questions |
|
|
|
|
| Q. No |
Question |
| 1 |
public class MyClass{
int x = 10;
static final int y= 5;
public void method (int z){
int a =
10;
final
float b = 10;
class
LocalClass {
int c = 3;
public void method (int d){
System.out.println (??);
// -- 1
}
}
public static void main (String args[]){
new MyClass();
}
}
Which of the following variable names can be replaced ?? at line
no marked as 1,without compiler error? |
| |
| A |
x |
| B |
a |
| C |
b |
| D |
c |
| |
|
|
A , C
and D |
 |
 |
Local classes can
access all the
variables from the
enclosing classes.
But it can access
only the final
variables of the
enclosing method or
the method
parameter. The above
constraint is
applicable if the
Local class is
declared inside the
non-static context.
since, if the Local
class is declared
inside the static
context it can
access only the
static members of
the enclosing class. |
 |
|
 |
|
|
| 2 |
public class MyClass{
public static void main (String args[]){
String
str = "Hello World";
System.out.print(str.indexOf('d'));
System.out.print(str.indexOf('h'));
System.out.print(str.indexOf('a'));
}
}
What will be the output? |
| |
| A |
10 -1
-1 |
| B |
9
-1 -1 |
| C |
10 0
-1 |
| D |
10 1
-1 |
| |
|
|
A |
 |
 |
IndexOf() returns
the index within
this string of the
first occurrence of
the specified
character. IndexOf()
method returns -1 if
it cannot find the
specified character.
The search is case
sensitive. So. it
returns -1 for
indexOf('a').
|
 |
|
 |
|
|
| 3 |
What is the range of values that can be stored
in a short primitive variable? |
| |
| A |
0 - 656535 |
| B |
-32768 to 32767 |
| C |
-32768 to 32768 |
| D |
-32767 to 32768 |
| |
|
|
B |
 |
 |
The short is a 16
bit signed integer.
Its value ranges
from -216
to 216-1 |
 |
|
 |
|
|
| 4 |
Which of the following lines will compile
without warning or error. |
| |
| A |
float f=1.3; |
| B |
int i=10; |
| C |
byte b=257; |
| D |
char c="a"; |
| |
|
|
B |
 |
 |
Always real values
default o double. So
when compiler sees a
value like 1.3 , it
will treat it as
double and will
complain. Choice C
is incorrect because
the byte value
ranges from -128 to
+127. Here also the
compiler will show
the same error
message,' possible
loss of precision'.
Choice D is
incorrect because ,
we cannot assign a
String to a char,
even though the
length is one. ie,
anything comes
inside " " is a
string. |
 |
|
 |
|
|
| 5 |
What will happen if you try to compile and run the
following code ?
public class MyClass {
public static void main (String
arguments[]) {
amethod( arguments) ;
}
public void amethod (String[]
arguments) {
System.out.println (arguments) ;
System.out.println (arguments[1]) ;
}
} |
| |
| A |
Error Can't make static
reference to void amethod. |
| B |
Error Incorrect main method. |
| C |
amethod must be declared
with String |
| D |
Compiles and executes fine. |
| |
|
|
A |
 |
 |
We cannot call a non
static method from a
static context. |
 |
|
 |
|
|
| 6 |
Which of the following will compile without
error ? |
| |
| A |
import java.util.*;
package mypack;
class MyClass {} |
| B |
package mypack;
import java.uril.*;
class MyClass{} |
| C |
/*This is a comment */
package MyPackage;
import java.awt.*;
class MyClass{} |
| D |
All of the above |
| |
|
|
B
and C |
 |
 |
The order of
different statements
and blocks in a java
program is as
follows. package
declaration first,
then imports, then
class declaration.
So if a package
declaration exist,
it =must be the
first non comment
code in the file. |
 |
|
 |
|
|
| 7 |
Float f= new Float( 1.0F );
String str = "value is " +f;
System.out.println( str );
What will be the output of executing the above code snippet? |
| |
| A |
Compiler error invalid
character in constructor |
| B |
Runtime exception invalid
character in constructor. |
| C |
Compiles and prints 'value
is 1.0' |
| D |
Compiles and prints 'value
is 1' |
| |
|
|
C |
 |
 |
Choice A and B is
incorrect because we
can use 'F' to
specify the given
number is float. |
 |
|
 |
|
|
| 8 |
Which of the following are java keywords
? |
| |
| A |
null |
| B |
const |
| C |
volatile |
| D |
true |
| |
|
|
B
and C |
 |
 |
Choices B and C are
java keywords.
Choice A and D are
not java keywords,
but they are treated
as reserved words in
java. |
 |
|
 |
|
|
| 9 |
What will be printed out if this code is run
with the following command line : java myprog good morning
public class myprog {
public
static void main (String argv[]) {
System.out.println(argv[2])
}
} |
| |
| A |
myprog |
| B |
good |
| C |
morning |
| D |
Exception raised: java.lang.ArrayIndexOutOfBoundsException:
2 |
| |
|
|
D |
 |
 |
Unlike
C++ , java command
line arguments does
not include the java
program name in the
argument list. So in
our program we
actually passing
only two arguments.
But when printing we
are printing the
third argument
(since array index
always start at
zero) and as a
result it will cause
a RuntimeException. |
 |
|
 |
|
|
| 10 |
What is the range of a byte |
| |
| A |
0 - 256 |
| B |
-128 to 127 |
| C |
-127 to 128 |
| D |
-255 to 256 |
| |
|
|
B |
 |
 |
The size of a byte
is 1 byte. So its
range is from -28
to +28-1. |
 |
|
 |
|
|
|
|
|