SCJP Questions

SCWCD Questions

Friday, April 10, 2009

SCJP Mock Questions : Declarations, Initialization and Scoping

********************************************************************
original from www.techfaq360.com
SCJP Mock Questions : Declarations, Initialization and Scoping
********************************************************************
Question No :1
Which statement is true?
(Choose correct one from multiple below)
1. A class may be declared final, in which case it cannot have subclasses .
2. A class can't declared final
3. A class may be declared final, in which case it can have subclasses
4. None of the above
correct is :1
Explanations :A class may be declared final, in which case it cannot have subclasses
------------------------------------------------------------------------------
Question No :2
Which statement is true?
(Choose correct one from multiple below)
1. outer class can only declare public , abstract and final
2. outer class may be private
3. outer class can't be abstract
4. None of the above
correct is :1
Explanations :outer class can only declare public , abstract and final
------------------------------------------------------------------------------
Question No :3
Is the bellow statement is true?
"abstract class cannot be instantiated, but can be extended by subclasses"
(Choose correct one from multiple below)
1. true
2. false
3. None of the above
4. None of the above
correct is :1
Explanations :abstract class cannot be instantiated, but can be extended by subclasses.
------------------------------------------------------------------------------
Question No :4
What is the output ?
public interface TestInf {
int i =10;
}
public class Test {
public static void main(String... args) {
TestInf.i=12;
System.out.println(TestInf.i);
}
}
(Choose correct one from multiple below)
1. Compile with error
2. 10
3. 12
4. None of the above
correct is :1
Explanations :All the variables declared in interface is Implicitly static and final , so can't change
the value.
------------------------------------------------------------------------------
Question No :5
What is the output ?
public class Test {
public static void main(String... args) {
final int i;
System.out.println(i);
}
}
(Choose correct one from multiple below)
1. Compile with error
2. 0
3. Runtime Exception
4. None of the above
correct is :1
Explanations :final variables should be initialized.
------------------------------------------------------------------------------
Question No :6
What will be the result of compiling the following code:
public class Test {
public static void main(String... args) {
static int i;
System.out.println(i);
}
}
(Choose correct one from multiple below)
1. the code will compile and print 0
2. the code will compile an print null
3. Complile time error.
4. None of the above
correct is :3
Explanations :static variables should not be local.
------------------------------------------------------------------------------
Question No :7
public class A extends Integer{
public static void main(Sring[] args){
System.out.println("Hello");
}
}
What is the output?
(Choose correct one from multiple below)
1. Hello
2. Compile Error
3. Runtime
4. None of the above
correct is :2
Explanations :final class can't be extended. Integer is final class.
------------------------------------------------------------------------------
Question No :8
private class B {
public static void main(String[] args){
System.out.println("DD");
B b = new B();
}
}
What is the output ?
(Choose correct one from multiple below)
1. DD
2. Compile Error.
3. Runtime Exception
4. None of the above.
correct is :2
Explanations :Only public, abstract and final is permitted for class modifier.
------------------------------------------------------------------------------
Question No :9
public class Point {
int x = 1, y = 1;
abstract void alert();
}
Is the code compile without error ?
(Choose correct one from multiple below)
1. compile without error
2. compile with error , because class should be abstract.
3. Can't say
4. None of the above.
correct is :2
Explanations :If there is any abstract method in a class then the class should be abstract.
------------------------------------------------------------------------------
Question No :10
abstract class Point {
int x = 1, y = 1;
abstract void alert();
}
public class A{
public static void main(String[] args){
Point p = new Point();
}
}
What is the output ?
(Choose correct one from multiple below)
1. compile without error
2. compile with error.
3. Can't say
4. None of the above.
correct is :2
Explanations :abstract class can't be instantiated.
------------------------------------------------------------------------------
Question No :11
Which bellow statement is true?
(Choose correct one from multiple below)
1. A subclass of an abstract class that is not itself abstract may be instantiated.
2. abstract class can't be instantiated.
3. Both are true
4. None of the above
correct is :3
Explanations :No explanation available
------------------------------------------------------------------------------
Question No :12
What is the correct ordering for the import, class and package declarations when found in a Java
class?
(Choose correct one from multiple below)
1. package, import, class
2. class, import, package
3. import, package, class
4. package, class, import
correct is :1
Explanations :Example :
package com;
import java.io.IOException;
public class Test {
}
------------------------------------------------------------------------------
Question No :13
What will be the result of compiling the following code:
public class Test {
public static void main (String args []) {
int age;
age = age + 1;
System.out.println("The age is " + age);
}
}
(Choose correct one from multiple below)
1. the code will compile an print 1.
2. the code will compile an print 0.
3. Complile time error.
4. None of the above
correct is :3
Explanations :local variable should be initialized.
------------------------------------------------------------------------------
Question No :14
What will be the result of compiling the following code:
public class Test {
static int age;
public static void main (String args []) {
age = age + 1;
System.out.println("The age is " + age);
}
}
(Choose correct one from multiple below)
1. The code does not compile.
2. The code compiles cleanly and print 1.
3. The code compiles cleanly and print 0.
4. The code throws an Exception at Runtime.
correct is :2
Explanations :static variables are initiatized automatically. default initialization value for int is 0 and
boolean is false.
------------------------------------------------------------------------------
Question No :15
Which of the following can be used to define a constructor for this class, given the following code:
public class Test {
...
}
(Choose correct one from multiple below)
1. public void Test() {...}
2. public Test() {...}
3. public static Test() {...}
4. public static void Test() {...}
correct is :1
Explanations :Constructor should not have any return type and should not be static.
------------------------------------------------------------------------------
Question No :16
Is this legal
public class Test {
static { int a = 5; }
public static void main(String[] args){
System.out.println(a);
}
}
(Choose correct one from multiple below)
1. Yes
2. No
3. Can't Say
4. None
correct is :2
Explanations :A variable declared in a static initialiser is not accessible outside its enclosing block.
------------------------------------------------------------------------------
Question No :17
class c2
{
{
System.out.println("initializer");
}
public static void main(String a[])
{
System.out.println("main");
c2 ob1=new c2();
}
}
(Choose correct one from multiple below)
1. prints main and initializer
2. prints initializer and main
3. compile time error
4. None of the above
correct is :1
Explanations :statement block executes on creation of object of the class.
------------------------------------------------------------------------------
Question No :18
public class A {
static{System.out.println("static");}
{ System.out.println("block");}
public A(){
System.out.println("A");
}
public static void main(String[] args){
A a = new A();
}
What will be output ?
(Choose correct one from multiple below)
1. A block static
2. static block A
3. static A
4. A
correct is :2
Explanations :First execute static block, then block then constructor.
------------------------------------------------------------------------------
Question No :19
class Bird {
{ System.out.print("b1 "); }
public Bird() { System.out.print("b2 "); }
}
class Raptor extends Bird {
static { System.out.print("r1 "); }
public Raptor() { System.out.print("r2 "); }
{ System.out.print("r3 "); }
static { System.out.print("r4 "); }
}
class Hawk extends Raptor {
public static void main(String[] args) {
System.out.print("pre ");
new Hawk();
System.out.println("hawk ");
}
}
What is the output?
(Choose correct one from multiple below)
1. r1 r4 pre b1 b2 r3 r2 hawk
2. r1 r4 pre b1 b2 hawk
3. r1 r4 pre b1 b2 hawk r3 r2
4. None of the above
correct is :1
Explanations :All static blocks execute first : sequesnce super class first
Blocks execute : sequesnce super class first
------------------------------------------------------------------------------
Question No :20
class Bird {
int i;
boolean b;
float f;
public Bird() {
System.out.println(i);
System.out.println(b);
System.out.println(f);
}
}
What is the output?
(Choose correct one from multiple below)
1. 0 false 0.0
2. 0 true 0.0
3. Not Compile
4. None of the above
correct is :1
Explanations :Jvm initialize default values;
Static vaiables also;
------------------------------------------------------------------------------
Question No :21
What is the result that will be printed out ?
void aMethod()
{
float f = (1 / 4) * 10;
int i = Math.round(f);
System.out.println(i);
(Choose correct one from multiple below)
1. 3
2. 0
3. 2
4. 2.5
correct is :2
Explanations :The result of 1/4 will be zero because integer
divion is carried out on the operands.
If you need to obtain a fractional value
you need to use either a float or double literal
as in 1F / 4F.
------------------------------------------------------------------------------
Question No :22
int a = 7;
int b = 4;
a = b;
a = a + 1;
System.out.println(a);
what will be the output ?
(Choose correct one from multiple below)
1. 8
2. 5
3. 7
4. 4
correct is :2
Explanations :No explanation available
------------------------------------------------------------------------------
Question No :23
What is the output?
System.out.println("The answer is: "+18+3);
(Choose correct one from multiple below)
1. 21
2. 183
3. Comiple Error
4. None
correct is :2
Explanations :No explanation available
------------------------------------------------------------------------------
Question No :24
class C {
public static void main(String[] args) {
int i1=1;
switch(i1){
case 1:
System.out.println("one");
case 2:
System.out.println("two");
case 3:
System.out.println("three");
}}}
(Choose correct one from multiple below)
1. prints one two three
2. prints one
3. compile time error
4. Runtime exceptionf
correct is :1
Explanations :There is no break statement in case 1 so it causes the below case statements to
execute regardless of their values
------------------------------------------------------------------------------
Question No :25
class C1
{
static class C2
{
static int i1;
}
public static void main(String a[])
{
System.out.println(C1.C2.i1);
}
}
(Choose correct one from multiple below)
1. prints 0
2. Compile time error
3. Runtime exception
4. None of the above
correct is :1
Explanations :static members can be accessed without instantiating the particular class
------------------------------------------------------------------------------
Question No :26
class C {
public static void main(String[] args) {
int[]a1[]=new int[3][3]; //3
int a2[4]={3,4,5,6}; //4
int a2[5]; //5
}}
(Choose correct one from multiple below)
1. compiletime error at lines 3,4,5
2. compiltime error at line 4,5
3. compiletime error at line 3
4. Runtime Exception
correct is :2
Explanations :no value shoud be specified in the rightsidebrackets when constructing an array
------------------------------------------------------------------------------
Question No :27
class C{
int i;
public static void main (String[] args) {
int i; //1
private int a = 1; //2
protected int b = 1; //3
public int c = 1; //4
System.out.println(a+b+c); //5
}}
(Choose correct one from multiple below)
1. compiletime error at lines 1,2,3,4,5
2. compiletime error at lines 2,3,4,5
3. compiletime error at lines 2,3,4
4. prints 3
correct is :2
Explanations :The access modifiers public, protected and private, can not be applied to variables
declared inside methods.
------------------------------------------------------------------------------
Distribution of this pdf is illegal without permission of techfaq360.com

No comments:

Post a Comment