Tuesday, August 10, 2010

Core java SCJP Mock test1

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.
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.







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.

No comments:

Post a Comment