Saturday, April 10, 2010

SCJP Questions

Question 1 :
class Demo{
public static void main(String args){
System.out.println("welcome to java");
}
}

What will be the output of the program:
a) Error at compile time b) Error at Runtime
c) prints welcome to java d) No Error and no output

===============================================================
Question 2:
Which of the following are legal statements in Java?
a) float f=1/3; b) float f=1.01;
c) int i=1/3; d) double d=999d;

===============================================================
Question 3:
What will happen when you compile and run the following code?
public class MyClass{
static int i;
public static void main(String argv[]){
System.out.println(i);
}
}
a) Error Variable i may not have been initialized c) 1
b) null d) 0

===============================================================
Question 4:
class Demo{
static void print(){
System.out.println("Hello");
}
public static void main(String args){
Demo d =null;
d.print();
}
}

a) Error at compile time b) Error at Runtime
c) prints Hello d) No Error and no output

===============================================================Question 5:
Which of the following statements are true?
a) A byte can represent between -128 to 127
b) A byte can represent between -256 to 256
c) A byte can represent between -127 to 128
d) A char can represent between -2x2 pow 16 2 x2 pow 16 - 1

===============================================================Question 6:
Which of the following statements are true?
a) Constructors cannot have a visibility modifier
b) Constructors can only have a primitive return type
c) Constructors can be marked public and protected, but not private
d) Constructors are not inherited

===============================================================Question 7:
What will happen when you attempt to compile and run the following class?
class Base{
Base(int i){
System.out.println("Base");
}
}
class Severn extends Base{
public static void main(String argv[]){
Severn s = new Severn();
}
void Severn(){
System.out.println("Severn");
}
}
a) Compilation and output of the string "Severn" at runtime
b) Compilation and no output at runtime
c) Compile time error
d) Compilation and output of the string "Base"

===============================================================Question 8:
Given the following code
class Base {}

class Agg extends Base{
public String getFields(){
String name = "Agg";
return name;
}
}
public class Avf{
public static void main(String argv[]){
Base a = new Agg();
//Here
}
}
What code placed after the comment //Here will result in calling the getFields method resulting in the output of the string "Agg"?
a) System.out.println(a.getFields());
b) System.out.println((Base) a.getFields());
c) System.out.println(a.name);
d) System.out.println( ((Agg) a).getFields());

===============================================================Question 9:
What will happen when you attempt to compile and run the following code?
public class Inc{
public static void main(String argv[]){
Inc inc = new Inc();
int i =0;
inc.fermin(i);
i = i++;
System.out.println(i);
}
void fermin(int i){
i++;
}
}
a) Compile time error b) Output of 1
c) Output of 2 d) Output of 0

===============================================================Question 10:
Given the following class
public class Ombersley{
public static void main(String argv[]){
boolean b1 = true;
if((b1 ==true) || place(true)){
System.out.println("Hello Crowle");
}
}

public static boolean place(boolean location){
if(location==true){
System.out.println("Borcetshire");
}
System.out.println("Powick");
return true;
}
}
What will happen when you attempt to compile and run it?
a) Compile time error
b) Output of Borcetshire and Powick followed by "Hello Crowle"
c) Output of "Hello Crowle"
d) No output
===============================================================Question 11:
What will happen when you attempt to compile and run the following code?
public class Sandys{
private int court;
public static void main(String argv[]){
Sandys s = new Sandys(99);
System.out.println(s.court);
}
Sandys(int ballcount){
court=ballcount;
}
}
a) Compile time error, the variable court is defined as private
b) Compile time error, s is not initialized when the System.out method is called
c) Compilation and execution with no output
d) Compilation and run with an output of 99
===============================================================
Question 12:
Given the following code
class Base{
static int oak=99;
}
public class Doverdale extends Base{
public static void main(String argv[]){
Doverdale d = new Doverdale();
d.amethod();
}
public void amethod(){
//Here
}
}
Which of the following if placed after the comment //Here, will compile and modify the value of the variable oak?
a) super.oak=1; b) Base.oak=22;
c) oak=33; d) oak=50.1;
===============================================================Question 13:
Which of the following statements are true?
a) A method cannot be overloaded to be less public in a child class
b) To be overridden a method only needs the same name and parameter types
c) To be overridden a method must have the same name, parameter and return types
d) An overridden method must have the same name, parameter names and parameter types
===============================================================Question 14:
You want to loop through an array and stop when you come to the last element. Being a good java programmer and forgetting everything you ever knew about C/C++ you know that arrays contain information about their size. Which of the following can you use?

a)myarray.length();
b)myarray.length;
c)myarray.size
d)myarray.size();
===============================================================Question 15:
What will happen when you attempt to compile and run this program
public class Outer{
public String name = "Outer";
public static void main(String argv[]){
Inner i = new Inner();
i.showName();
}//End of main
private class Inner{
String name =new String("Inner");
void showName(){
System.out.println(name);
}
}//End of Inner class
}
a) Compile and run with output of "Outer"
b) Compile and run with output of "Inner"
c) Compile time error because Inner is declared as private
d) Compile time error because of the line creating the instance of Inner
===============================================================Question 16:
Given the following class definition, which of the following methods could be legally placed after the comment //Here

public class Rid{
public void amethod(int i, String s){}
//Here
}
a)public void amethod(String s, int i){}
b)public int amethod(int i, String s){}
c)public void amethod(int i, String mystring){}
d) public void Amethod(int i, String s) {}
===============================================================Question 17:
public class Test7{
public Test7(){}
public Test7(Test7 ref){
this (ref,"Hai");
}
public Test7(Test7 ref,String str){
ref.Test7(str);
System.out.println("Hi");
}
public void Test7(String str){
System.out.println(str);
}
public static void main(String[] args){
Test7 t = new Test7();
Test7 t7 = new Test7(t);
}
}
a) HI b) hai
c) Hai Hi d) Hi Hai
===============================================================Question 18:
What will be the output of the following program?

class Sup1{
public Sup1(){
System.out.println("Hai");
}
private Sup1(String str){
System.out.println(str);
}
}

public class Test5 extends Sup1{
private Test5(String str){
System.out.println(str);
super();
}
public static void main(String[] args) {
Test5 t5 = new Test5("HI");
}
}

a) Hai,Hi,Hi b) Hai,Hi
c) Hi,Hi d) Compiler Error
===============================================================Question 19:
class VarArgOne {
public static void printArgs(String s, int ... i, String s) { //line1
for(int j : i) { //line 2
System.out.print(j + " " + s); //line 3
}
}
public static void main(String ... args) { //line 4
printArgs("exam", 12, 34, "scjp"); //line 5
}
}
a) Compilation fails due to error at line 1.
b) Compilation fails due to error at line 2.
c) Compilation fails due to error at line 4.
d) Compilation fails due to error at both line 1 and line 4.
e) Compiles fine and Prints output "12 scjp 34 scjp".
===============================================================Question 20:
class A{
private void print(){
System.out.println("===print in A===");
}
}
class B extends A{
void print(){
System.out.println("===print in B===");
}
}
class M{
public static void main(String args[]){
A ref = new B();
ref.print();
}
}
What happens when we try to compile the above program:
a)Error at compile time b)Error at run time
c)prints print in A d) prints print in B
===============================================================Question 21 :
interface I{
void print();
}
class A{
static void print(){
System.out.println("===print in A===");
}
}
class B extends A implements I{
public void print(){
System.out.println("===print in A===");
}
}
class M{
public static void main(String args[]){
A ref = new B();
ref.print();
}
}
What will be the output & Write explanation?
===============================================================Question 22:
What all gets printed when the following code is compiled and run? Select the three correct answers.
public class xyz {
public static void main(String args[]) {
for(int i = 0; i < 2; i++) {
for(int j = 2; j>= 0; j--) {
if(i == j) break;
System.out.println("i=" + i + " j="+j);
}
}
}
}
===============================================================Question 23:
What is the result of compiling and running the following program. Select the one correct answer.
class test {
public static void main(String args[]) {
int[] arr = {1,2,3,4};
call_array(arr[0], arr);
System.out.println(arr[0] + "," + arr[1]);
}
static void call_array(int i, int arr[]) {
arr[i] = 6;
i = 5;
}
}
a. 1,2 b. 5,2
c. 1,6 d. 5,6
===============================================================Question 24:
Assume that class A extends class B, which extends class C. Also all the three classes implement the method test(). How can a method in a class A invoke the test() method defined in class C (without creating a new instance of class C). Select the one correct answer.
a. test();
b. super.test();
c. super.super.test();
d. ::test();
e. C.test();
f. It is not possible to invoke test() method defined in C from a method in A.
===============================================================Question 25:
Which of the following are legal array declarations. Select the three correct answers.
a. int i[5][]; b. int i[][];
c. int []i[]; d. int i[5][5];
e. int[][] a;
===============================================================
ALL THE BEST

No comments:

Post a Comment