Monday, April 13, 2009

100 questions in java

scjp questions


What is the output of this program
Q 1 class BoxAndWiden {
static void go(Object o) {

Integer i2 = (Integer) o;
System.out.println(i2);

}
public static void main(String [] args) {


go(1000);

}
}

1 compile time error
2 Runtime Error
3 1000
4 No output
3

Q 2
public class CheckIt{
int i;
CheckIt(){
i+=10;
}
public static void main(String[] args){
CheckIt t = new CheckIt();
System.out.print("value 1 = " +t.i);
CheckIt t1 = new CheckIt();
System.out.print(" value 2 = " +t1.i);
}
}

1 compile time error
2 Runtime Error
3 10 , 10
4 10 , 20
3


Q 3 what is the ouput

public class Demo1 {
String title;
int value;
public Demo1() {
title += " Java ";
}
public Demo1(int value) {
this.value = value;
title = "World ";
title= title.concat("Inside");
}
void Demo1(){
System.out.println("Running ");
}
public static void main(String a[]){
Demo1 c = new Demo1(5);
System.out.println(c.title);
}
}

1 compile time error
2 Runtime Error
3 Java
4 World
5 World Inside
6 None of these
5





Q 4 what is the output

public class Hello {
String title;
int value;
public Hello() {
title += " Java ";
}
public Hello(int value) {
this.value = value;
title = "World ";
title.concat("Inside");
}
void Hello(){
System.out.println("Running ");

}

public static void main(String a[]){
Hello c = new Hello(5);
System.out.println(c.title);
}
}



1 compile time error
2 Runtime Error
3 World
4 Java
5 None of these
3






Q 5 what is the output ?

public class PrintIt{
public static void main(String[] args){
int a;
PrintIt t = new PrintIt();
for(int j=0; j < 2 ; j++){
for(int i=0; i <4 ; i++){
a = j;

}

System.out.println(i);

}
}
}




1 compile time error
2 Runtime Error
3 prints 0 ,1 ,2,3
4 pirnts 0,1,2,3,4
5 None of these
1



Q 6 what is the output
public class TestArrayAObject{
int arr[] = new int[8];
public static void main(String[] args){
TestArrayAObject t = new TestArrayAObject();
System.out.println(t.arr[7]);
}
}



1 -0
2 compile time error
3 Runtime Exception
4 -1
5 None of these
1


Q 7 what is the output –

public class TestCAses {
public static void main(String[] args){
char c = '\u0044';
switch(c) {
default:
System.out.println("Default");
case 'A':
System.out.println("A");
case 'C':
System.out.println("C");
case 'B':
System.out.println("B");
}
}
}




1 compile time error
2 Runtime Error
3 default C A B
4 default B A C
5 default A C B
6 None of these
5


Q -8 what is the output

class Vararg { // 1
static void method1(long... a)//2
{ System.out.println("long type "); }//3
static void method1(int a,long... b)//4
{ System.out.println("int and long type "); //5
}

static void method1(Integer... a)// 6
{ System.out.println("Integer type "); }//7
public static void main(String [] args) {//8
method1(5,5); // 9
}
}




1 compile time error in line 1
2 compile time error in line 3
3 compile time error in line 7
4 compile time error in line 9
5 Runtime Error
6 None of these
4



Q 9

class WidenAndBox {
static void go(Long x) { System.out.println("Wrapper Long type"); }
public static void main(String [] args) {
long b = 5;
go(b);
}
}

1 compile time error
2 Runtime Error
3 Wrapper Long type
4 None of these
3


Q 10

public class Boxing1 {
public static void main(String[] args) {
Integer i = null;
method(i);
}
static void method(Object o){
if(o==null)
System.out.println("dont put null");
}
}




1 compile time error
2 Runtime Error
3 don put null
4 none of these
3







Q 11


public class Boxing2 {
public static void main(String[] args) {
byte b = 10;
method(b);
}
static void method(int i){
System.out.println("Primitivae Type call");
}
static void method(Integer i){
System.out.println("Wrapper Type Call");
}
}

1 compile time error
2 Runtime Error
3 Primitivae Type call
4 Wrapper Type Call
5 None of these
3

Q 12


public class Boxing3 {
public static void main(String[] args) {
int i = 10;
method(i);
}
static void method(Long l){
System.out.println("Widening conversion");
}
}

1 compile time error
2 Runtime Error
3 Widening conversion
4 None of these
1




Q 13

public class Boxing4 {
public static void main(String[] args) {
Integer i = 10;
Integer j = 10;
System.out.print(i==j);// trure
System.out.print(i.equals(j));// true
System.out.print(i != j);// false

}
}

1 compile time error
2 Runtime Error
3 true false ,false
4 true , true , false
5 true , true , true
6 false , false , trure
7 None of these
4



Q 14
public class Boxing5 {
public static void main(String[] args) {
Integer i = 128;
Integer j = 128;
System.out.print(i==j);//s false
System.out.print(i.equals(j));// true
}
}




1 compile time error
2 Runtime Error
3
4 false true
5 None of these
4





Q 15

public class Boxing6 {
public static void main(String[] args) {
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(true);
boolean b3 = true;
Boolean b4 = true;
System.out.println(b1==b2);//f
System.out.println(b1==b3);//t
System.out.println(b3 == b4);//t
System.out.println(b1 == b4);//f
}
}




1 compile time error
2 Runtime Error
3
4
5 false true true , false
6 None of these
5

Q 16
public class Boxing7 {
public static void main(String[] args) {
int i = 10;
method(i);
Integer m =12;
method(m);

}
static void method(long l){
System.out.println("long called");
}
static void method(Integer i){
System.out.println("Integer called");
}
}




1 compile time error
2 Runtime Error
3 long called , Integer called
4 Integer called , long called
5 None of these
3





Q 17

public class Boxing8 {
public static void main(String[] args) {
Integer i = 10;
int k = 10;
method(i,k);

}
static void method(int i,Integer k){
System.out.println("1 int,Integer called");
}
static void method(int i,int k){
System.out.println("int,int called");;
}
static void method(Integer i,Integer k){
System.out.println("Integer,Integer called");;
}
static void method(Integer i,int k){
System.out.println(" Integer,int called");;
}
}

1 compile time error
2 Runtime Error
3 Integer,Integer called
4 Integer,int called
5 None of these
6 int,Integer called
4



Q 18



public class Boxing9 {
public static void main(String[] args) {
int i = 10;
method(i);
}

static void method(Object o){
System.out.println("Object called");
}
static void method(Number n){
System.out.println("Number called");
}
}


1 compile time error
2 Runtime Error
3 Object called
4 Number called
5 None of these
4





Q 19

import java.util.*;
class CheckHashSet{
public static void main(String ar[])
{

HashSet hs = new HashSet();
hs.add("scjp"); // 1
hs.add("exam"); //2
HashSet s = new HashSet();
s=hs; //3
System.out.println(s);
}
}




1 compile time error in line 1
2 compile time error in line 2
3 compile time error in line 3
4 Runtime Error
5 None of these
3

Q 20

public class Boxing10 {
public static void main(String[] args) {
int i = 10;
int k = 20;
method(i,k);
}
static void method(Integer... i){
System.out.println("Integer varargs is called");
}
static void method(Integer i,Integer j){
System.out.println(" Integer,Integer is called");
}
static void method(int... i){
System.out.println("int varargs is called");
}
}




1 compile time error
2 Runtime Error
3 int varargs is called
4 Integer,Integer is called
5 Integer varargs is called
6 None of these
4





Q 21 what is the ouput

public class CollectioinOverloading{

public static void method(float f){
System.out.println("Float");
}

public static void method(double f){
System.out.println("Double");
}
public static void main(String a[]){
float f1 = 2.0f;
float f2 = 2.0f;
method(1.0);//double
method(1.0f);// flt
method(1.0f*2.0f);// F
method(1.0f*2.0);// double

method(f1*f2);//F


}
}




1 compile time error
2 Runtime Error
3 double , float ,float , double, float
4 double , float ,float , float , double
5 None of these
6 float ,float , float , double , float
3




Q 22

import java.util.*;

public class DiffObeInAnArray{
public static void main(String ar[])
{
Object [] myObjects = {
new Integer(12),
new Integer(5),
new Boolean(true)
};
Arrays.sort(myObjects);

for(int i=0; i System.out.print(myObjects[i].toString());
System.out.print(" ");
}
}}



1 compile time error
2 classCastException
3 NullpoinerExceptin
4 pirnt all values
5 None of these
2






Q 23
import java.util.*;
public class Example {
public static void main(String[] args) {


Set set = new LinkedHashSet();

set.add(new Integer(3));

set.add(new Integer(2));
set.add(new Integer(2));


System.out.println(set);
}
}


1 compile time error
2 Runtime Error
3 - 3,2
4 3,2 2
5 -2,3
6 2,2,3
7 None of these
3




Q 24

class FinalCheck1{
public static void main(String ar[]){

//System.out.println(i);


}

static byte m2(final int i){
return i ;
}

}


1 compile time error
2 Runtime Error
3
4
5 None of these
1






Q 25
import java.util.*;

public class LinkedListsorting {

public static Collection get() {
Collection sorted = new LinkedList();
sorted.add("B"); sorted.add("C"); sorted.add("A");
sorted.add("A");
return sorted;
}

public static void main(String[] args) {


for (Object x : get()) {
System.out.print(x + ", ");
}
}
}



1 compile time error
2 Runtime Error
3 B , C, A ,
4 C , C B A
5 None of these
3


Q 27

import java.util.*;
public class TestLInkedHashMap{
public static void main(String a[]){
Map s = new LinkedHashMap();
s.put("1","one");
s.put("3","three");
s.put("2","two");

System.out.println(s);
}
}




1 compile time error
2 Runtime Error
3 1 = one , 3 =thre , 2 =two
4
5 None of these
3





Q 28


public class Test23{
public static void main(String a[]){
String s1 = "Sun";
System.out.println(s1.substring(0,2));
}
}


1 compile time error
2 Runtime Error
3 Sun
4 Su
5 un
6 None of these
4


Q 29
public class Test14{
static String s ="Instance";

public static void method(String s){
s+="Add";
}
public static void main(String a[]){
Test14 t = new Test14();
s = "New Instance";
String s = "Local";
method(s);
System.out.println(s);// local
System.out.println(t.s);//new instance
}
}




1 compile time error
2 Runtime Error
3 Local
4 Local ,new Instance
5 None of these
4





Q 30


public class Test13{
public static void method(String s){
System.out.println("String Version");
}

public static void method(StringBuffer sb){
System.out.println("String Buffer Version");
}

public static void main(String a[]){
method(null);
}
}





1 compile time error
2 Runtime Error
3
4
5 None of these
1


Q 31



import java.util.*;
class TestTreeSet1{
public static void main(String a[]){
Set s = new TreeSet();
s.add(new Person(20));
s.add(new Person(10));
System.out.println(s);
}
}
class Person{
Person(int i){}
}

1 compile time error
2 Runtime Error
3
4
5 None of these
2



Q 32

public class WaitTest {
public static void main(String [] args) {
System.out.print("1 ");
synchronized(args){
System.out.print("2 ");
try {
args.wait();
}
catch(InterruptedException e){}
}
System.out.print("3 ");
} }





1 compile time error
2 Runtime Error
3 1 2 and waits forever
4 2 1 and waits forever
5 None of these
3




Q 33

class DobleRunInThread{

public static synchronized void main(String[] args) throws
InterruptedException {
Thread t = new Thread();
t.start();
System.out.print("X");
t.wait(10000);
System.out.print("Y");
}
}





1 compile time error
2 Runtime Error
3
4
5 None of these
2




Q 34
public class TwoThreads {
static Thread laurel, hardy;
public static void main(String[] args) {
laurel = new Thread() {
public void run() {
System.out.println("A");//
try {
hardy.sleep(1000);
} catch (Exception e) {
System.out.println("B");
}
System.out.println("C");//
}
};
hardy = new Thread() {
public void run() {
System.out.println("D");//
try {
laurel.wait();
} catch (Exception e) {
System.out.println("E");//
}
System.out.println("F");//
}
};
laurel.start();
hardy.start();
}
}





1 compile time error
2 Runtime Error
3 A D E F C
4
5 None of these
3

Q 35

public class Messager implements Runnable {
public static void main(String[] args) {
new Thread(new Messager("ONE ")).start();
new Thread(new Messager("TWO ")).start();
}
private String name;
public Messager(String name) { this.name = name; }
public void run() {
message(1);
message(2);
}
private synchronized void message(int n) {
System.out.print(name + "-" + n + " ");
}
}


1 compile time error
2 Runtime Error
3
4 ONE -1 , TWO-1 , ONE -2 , TWO -2
5 None of these
4




Q 36



class ThreadTest extends Thread
{
public void run()
{
for(int i=0;i<100;i++)
{
System.out.println(i);
}
}
public static void main(String[] args) throws InterruptedException
{
ThreadTest tt = new ThreadTest();

tt.start();
tt.interrupt();

System.out.println("INTRUPTED ");

}
}




1 compile time error
2 Runtime Error
3 PRINT 1 TO 99 AND INTRUPTED IN BETWEEN RANDOMELY
4
5 None of these
3

Q 37

public class Threads4 implements Runnable {
public void run() {
System.out.println(" This is the thread task ");
}
public void run (String s ) {
System.out.println(" this is " +s);
}
public static void main(String[] args) {
Threads4 t4 =new Threads4();
t4.run("Customer -");
Thread t1= new Thread(t4);
t1 .start();
}
}





1 compile time error
2 Runtime Error
3
4 this is customer , this is the thread task
5 None of these
4


Q 38
class Animal { public String noise()
{
return "peep";
}
}

class Dog extends Animal {
public String noise() { return "bark"; }
}

class Cat extends Animal {
public String noise() { return "meow"; }
}


public class OverridingTest{
public static void main(String a[]){

Animal animal = new Dog();
Cat cat = (Cat)animal;
System.out.println(cat.noise());
}
}





1 compile time error
2 Runtime Exception
3
4
5 None of these
2

Q 39
public class Threads3 implements Runnable {
public void run() {
System.out.print("executing ");
}
public static void main(String[] args) {
Thread t1 = new Thread(new Threads3());
t1.run();
t1.run();
t1.start();
}
}





1 compile time error
2 Runtime Error
3 executing , executing , executing
4
5 None of these




Q 40

public class Threads2 implements Runnable {

public void run() {
System.out.println("running .");
throw new RuntimeException("Problem");
}

public static void main(String[] args) {

Thread t = new Thread(new Threads2());
t.start();
System.out.println("closing ");
}
}


1 compile time error
2 Runtime Error
3 prints randomly
4
5 None of these
3






Q 41

class Thread1 implements Runnable
{
public void run()
{

synchronized(this){
for(int i=0;i<5;i++)
{

System.out.println("In Thread1 "+Thread.currentThread().getName() +" Is Executing");
}

try
{
notifyAll();
}
catch(Exception e)
{
System.out.println(e);
}

}

}
}


public class ThreadJoin
{
public static void main(String [] arg)
{
Thread1 t1=new Thread1();


Thread th1=new Thread(t1);
th1.setName("A");
Thread th2=new Thread(t1);
th2.setName("B");
th1.start();
th2.start();

synchronized(th1){
try{
th1.wait();
}catch(InterruptedException ex){
}
}

synchronized(th2){
try{
th2.wait();
}catch(InterruptedException ex){
}
}


System.out.println("last line in main");


}
}

1 compile time error
2 Runtime Error
3 A , A B B OR B , B A , A AND MAIN OR B B , AA AND WAITING
4
5 None of these
3

Q 42
class MyThread extends Thread {

MyThread() {
System.out.print(" MyThread CONSTRUCTOR ");
}

public void run() { System.out.print(" NO ARG RUN"); }

public void run(String s) { System.out.print(" RUN WITH STRING "); }
}

public class TestThreads {

public static void main (String [] args) {
Thread t = new MyThread() {
public void run()
{
System.out.print(" HELLO WORLD ");
}
};
t.start();
}
}


1 compile time error
2 Runtime Error
3 MY THREAD , CONSTRUCTOR , HELLO WORL
4
5 None of these
3




Q 43


public class TestOne {

public static void sleepCheck()
{
try{
Thread.sleep(1000);
}
catch(Exception e ){}
}

public static void main (String[] args) throws Exception {
System.out.println("sleeping 1 ");

Thread.sleep(4000);
System.out.println("sleeping 2");
}
}





1 compile time error
2 Runtime Error
3 sleeping 1 " sleep for 4 seconds and sleeping 2
4
5 None of these
3



Q 44
class TestLock implements Runnable
{
private int a;
public void run()
{
for(int i=0;i<3;i++)
up();
}
public synchronized void up()
{
a=a+1;
System.out.println("a is - "+a);
}
public static void main(String[] args)
{

TestLock ta=new TestLock();
TestLock tb=new TestLock();
TestLock tc=new TestLock();

Thread t=new Thread(ta);
Thread to=new Thread(ta);
Thread to1=new Thread(ta);

t.start();
to.start();
to1.start();
}
}

1 compile time error
2 Runtime Error
3 a is 1 to 9 ( is printed)
4
5 None of these
3



Q 45

class Top {

Top()

{
System.out.print("top ");
}

public Top(String s) { System.out.print("B");
}

}


public class Bottom2 extends Top {
public Bottom2(String s)
{
System.out.print("D");
}
public static void main(String [] args) {
new Bottom2("C");
}
}






1 compile time error
2 Runtime Error
3 top d
4
5 None of these
3

Q 46


class A {
public int Instances;
public A(int Instances) {
this.Instances = Instances;
} }

class B extends A {
protected B(int Instances) { super(Instances);}
}
public class C extends B {
private C(int Instances) {
super(Instances); }
public static void main(String[] args) {
C ext = new C(100);
System.out.print(ext.Instances);
}
}






1 compile time error
2 Runtime Error
3 100
4
5 None of these
3



Q 47

public class Test {
public static void main (String [] args) {
final Foo f = new Foo();
Thread t = new Thread(new Runnable() {
public void run() {
f.doWork();
}
});

Thread g = new Thread() {
public void run() {
f.doWork();
}
};
t.start();
g.start();
}
}
class Foo {
int x = 5;
public void doWork() {
if (x < 10) {
// nothing to do
try {
wait();
} catch(InterruptedException ex) { }
} else {
System.out.println("x is " + x++);
if (x >= 10) {
notify();
}
}
}
}

1 compile time error
2 IllegalMonitorExceptin
3
4
5 None of these
2



Q 48
class Key{
public static void go() {
System.out.println("go in key");
}
}
class Board extends Key {
public static void go() {
System.out.println("go in board");
}
}
public class StaticMethodInheritence{
public static void main(String ar[])
{
Key k= new Key();
Board b= new Board();
b= (Board)k;
k.go();
}
}





1 compile time error
2 ClassCastException
3
4
5 None of these
2




Q 49

public class Starter extends Thread
{
private int x= 2;
public static void main(String[] args) throws Exception
{
new Starter().makeItSo();
}
public Starter()
{

start();
}
public void makeItSo() throws Exception
{
try{
Thread.sleep(1000);
}catch(Exception ex){

}

x=x- 1;
System.out.println(x);
}
public void run()
{
x *= 3;
}
}





1 compile time error
2 Runtime Error
3 sleep for 1 second and print 5
4
5 None of these
3



Q 50
import java.util.*;
public class PrQu {
public static void main(String[] args) {
PriorityQueue pq1 = new PriorityQueue();
pq1.add("carrot");
pq1.add("apple");
pq.add("banana");

System.out.println(pq1.poll() + ":" + pq.peek());
}
}

1 compile time error
2 Runtime Error
3 apple : banana
4
5 None of these
4






Q 51
public class Hello {
String title;
int value;
public Hello() {
title += " World";
}

public Hello(int value) {

this.value = value;
title = "Hello";
new Hello();

}

public static void main(String ar[])
{
Hello h = new Hello();
System.out.println(" "+ h.title);
}
}





1 compile time error
2 Runtime Error
3 null world
4
5 None of these
3


Q





1 compile time error
2 Runtime Error
3
4
5 None of these




Q 52
class Animal { }
class Dog extends Animal {
void doDogStuff()
{
System.out.println("dogs stuff ");
}
}
public class DogTest {
public static void main(String [] args) {
Animal a = new Dog();
Dog d = (Dog) a;
d.doDogStuff();

((Dog)a).doDogStuff();
}
}



1 compile time error
2 Runtime Error
3 dogs stuff
4
5 None of these
3


Q 53

interface Bounceable{
void bounce();
void setBounceFactor(int bf);
}
abstract class Ball implements Bounceable {
public abstract void hello();
public static void go(){
System.out.println("static method in abst class ");

}
public void bounce() {
System.out.println("bounce in abstract class");
}
public void setBounceFactor(int bf) {
System.out.println("set boucne factor in abst class ");
}
}
public class SoccerBall extends Ball {
public void hello()
{

}

public void bounce() {
System.out.println("bounce in soccer class");
} // Define bounce behavior
public void setBounceFactor(int bf) {
System.out.println("set boucne factor in soccer class ");
}
public static void main(String ar[])
{
SoccerBall sb= new SoccerBall ();
sb.bounce();
Ball.go();
}
}


1 compile time error
2 Runtime Error
3 bounce in soccer class , static method in abst class
4
5 None of these
3

Q 54

class charAndInt {
public static void main(String [] args) {
char x ='m';
int y=x;
System.out.println("y is "+y);
System.out.println("y is "+(char)y);
char a= 'm';
long l=a;
System.out.println("l is "+l);
System.out.println("l is "+(char)l);
char e= 'o';
short s =(short)e;
System.out.println("e is "+e);
System.out.println("e is "+(char)e);
}
}


1 compile time error
2 Runtime Error
3 y is 109 ,m 109,m,,o,o
4
5 None of these
3

Q 55
enum Dogs {co, ha, sh}
public class Test {

public static void main(String [] args) {
Dogs myDog = Dogs.co;

switch (myDog) {
case sh: System.out.print("SH ");
case co: System.out.print("CO ");
case ha: System.out.print("HA ");
default: System.out.println("DO");

}
}

}







1 compile time error
2 Runtime Error
3 CO HA DO
4
5 None of these
3



Q 56
enum Color {red, green, blue}

class SwitchEnum {
public static void main(String [] args) {
Color c = Color.green;
switch(c) {

case red: System.out.print("red ");
case green: System.out.print("green ");
case blue: System.out.print("blue ");
default: System.out.println("done");
}
}
}





1 compile time error
2 Runtime Error
3
4 green blue done
5 None of these
4
Q 57

enum Size {
SMALL{
public String toString(){
return "Small Size";
}
} ,
Medium ,

LARGE{
public String toString(){
return "Large Size";
}
};

}
class SizeTest{
public static void main(String[] args) {
System.out.println(Size.SMALL.toString());
System.out.println(Size.Medium.toString());
System.out.println(Size.LARGE.toString());
}
}





1 compile time error
2 Runtime Error
3 small size , medium size, large size
4
5 None of these
3



Q 58





1 compile time error
2 Runtime Error
3
4
5 None of these

Q 59

enum Season {
WINTER,
SUMMER,
SPRING,
AUTUMN
}
class SeasonTest{
public static void main(String[] args) {
Season season1 = Season.WINTER;
Season season2 = Season.SUMMER;
Season season3 = Season.SPRING;
Season season4 = Season.AUTUMN;

System.out.println(season1.equals(season2));//f
System.out.println(season3.equals(season4));//f
System.out.println(season3.equals(season3));//t
}
}


1 compile time error
2 Runtime Error
3 false , false , true
4
5 None of these
3


Q 60
//
public class StaticVaribleNotUsedOutSide {
static {
int a = 5;

}
static int a=9;

public static void main(String[] args){

System.out.println(a);
}
}





1 compile time error
2 Runtime Error
3 - 9
4
5 None of these
3


Q 61
class A
{
static void staticMethod()
{
System.out.println("static me");
}
void nonStaticMethod()
{
System.out.println("non- static ");
A.staticMethod();
}
}
class MainClass
{
public static void main(String[] args)
{
new A().nonStaticMethod();
}
}




1 compile time error
2 Runtime Error
3 non static , static me
4
5 None of these
3



Q 62

public class InstanceInitializer
{ public static void main (String [] args) {
Sub subobj = new Sub ();
System.out.println ("super is "+ super.initvar);
Super s1 = new Sub();
System.out.println ("initvaribal is "+ s1.initvar);
}
}
class Super
{
public int initvar = 100;
Super () {
{
initvar=this.initvar;
}
System.out.println ("In Super's constructor. initvar value = " + initvar);
}
}
class Sub extends Super
{
public int initvar = 200;
Sub () {
super ();
System.out.println ("Done calling super() in Sub class");
}
}





1 compile time error
2 Runtime Error
3
4
5 None of these
1

Q 63
public class inheritex
{
int i=5;
}

class inheritex1 extends inheritex
{
int i=3;
public static void main(String args[])
{
inheritex1 ex=new inheritex1();
System.out.println(ex.i);
}
}


1 compile time error
2 Runtime Error
3 - 3
4
5 None of these
3


Q 64
enum Drinks {
COFFEE("hot", 10.0), TEA("hot", 5.0), JUICE("cold", 20.0), LASSI(8.0);
String state;
double price;

private Drinks(String s, double p) {
state = s;
price = p;
}
private Drinks(double p) {
price = p;
}
private Drinks(Double p) {
price = p;
}
public String toString() {
return "This Item is " + state + " and costs $" + price;
}
}
class EnumThree {
public static void main(String[] args) {
System.out.println(Drinks.LASSI);
System.out.println(Drinks.JUICE);
}
}


1 compile time error
2 Runtime Error
3
4 This Item is null and cost is $ 8.0, This Item is cold and cost is $ 20.0
5 None of these
4

Q 65

import java.util.*;
class GEnes{
public static void main(String[] args) {
List al = new ArrayList(); //line 1
al.add(12.2); //line 2
for (Object no:al) //line 3-
{System.out.println(no);}

}
}

1 compile time error
2 Runtime Error
3
4
5 None of these
1



Q 66

enum Drinks {
COFFEE("hot", 10.0), TEA("hot", 5.0), JUICE("cold", 20.0), LASSI(8.0);
String state;
double price;

private Drinks(String s, double p) {
state = s;
price = p;
}
private Drinks(double p) {
price = p;
}
private Drinks(Double p) {
price = p;
}
public String toString() {
return "This Item is " + state + " and costs $" + price;
}
}
class EnumThree {
public static void main(String[] args) {
System.out.println(Drinks.LASSI);
System.out.println(Drinks.JUICE);
}
}





1 compile time error
2 Runtime Error
3
4
5 None of these

Q 67
enum Position {
FIRST, SECOND, THIRD;
public static void main(String[] args) { //line 1
Position p = Position.THIRD; //line 2
switch(p) {
default :
System.out.println("Better luck defluat time");
break;
case FIRST : //line 4
System.out.println("Congrats, You are the FIRST winner");
//line 6
case SECOND :
System.out.println("Congrats, You are the second winner ");


}
}
}


1 compile time error
2 Runtime Error
3 Better luck defluat time"
4
5 None of these
3




Q 68

class Outer {
// class has static enum and static inner class


static enum OuterEnum { OUT, OUTTER} //line 1


static class Inner { //line 2
enum InnerEnum { IN, INNER} //line 3
}

public static void main(String [] args) {
System.out.println(Outer.OuterEnum.OUT + " " + Outer.Inner.InnerEnum.IN); //line 6
}
}

1 compile time error
2 Runtime Error
3 OUT ,IN
4
5 None of these

Q 69
class EnumEight {
enum Shape {
CIRCLE("zero"), RECTANGLE("four") ,SQUARE("four") ,TRIANGLE("three");
String sides;
private Shape(String s) {
sides = s;
}
public String toString() {
return sides;
}
}
public static void main(String [] args) {
System.out.println(Shape.CIRCLE + " " + Shape.RECTANGLE + " " + Shape.SQUARE + " " + Shape.TRIANGLE);
}

}


1 compile time error
2 Runtime Error
3 zero , four , four three
4
5 None of these
3




Q 70
enum Seasons {
WINTER,
SUMMER,
SPRING,
AUTUMN;
}
class SeasonsTest10{
public static void main(String[] args) {
Seasons season1 = Seasons.SUMMER;
Seasons season2 = Seasons.WINTER;
Seasons season3 = Seasons.SPRING;
Seasons season4 = Seasons.AUTUMN;

System.out.println(season1.compareTo(season2));//
System.out.println(season3.compareTo(season4));//

System.out.println(season4.compareTo(season3));
System.out.println(season2.compareTo(season1));
System.out.println(season3.compareTo(season3));
System.out.println(season1.compareTo(season4));
}
}


1 compile time error
2 Runtime Error
3 1 , -1 ,1 , -1 , 0 , - 2
4
5 None of these
3

Q 71

import java.util.*;
class TestGen
{
public static void main(String[] args)
{
//List list = new ArrayList();
List list = new ArrayList();
list.add(new BullDog ());
}
}
class Animal
{}
class Cat extends Animal{}
class Dog extends Animal{}
class BullDog extends Dog{}
class CowDog extends BullDog{}






1 compile time error
2 Runtime Error
3
4
5 None of these
1




Q 72



enum Direction {
SOUTH("eastern"),WEST("western"),EAST("northen"),

NORTH("southen"){
public String toString() { return "HELLO"; }
};

String side;
private Direction(String d) {
side = d;
}
public String toString() {
return "HI";
}

}
class EnumTwo {
public static void main(String[] args) {
System.out.println(Direction.NORTH + " " + Direction.WEST + " " + Direction.EAST + " " + Direction.SOUTH);
}
}





1 compile time error
2 Runtime Error
3
4 HELLO , HI , HI , HI
5 None of these

Q 73
import java.util.*;
class priQue{
public static void main(String ar[]){

PriorityQueue pq = new PriorityQueue();
pq.add("2");
pq.add("4");
pq.add("3");
pq.add("1");
pq.add("10");
System.out.println(pq);
PriorityQueue pq1 = new PriorityQueue();
pq1.add("2");
pq1.add("3");
pq1.add("5");
pq1.add("6");

pq1.add("4");
System.out.println(pq1);

}
}





1 compile time error
2 Runtime Error
3 ,10 ,3,4, 2,, 2 , 3 , 5 , 6 , 4
4
5 None of these




Q 74



enum Colors {
RED,
GREEN,
BLUE
}
class ColorsTest1{
public static void main(String[] args) {
System.out.println(Colors.GREEN.ordinal());//
System.out.println(Colors.BLUE.ordinal());//
System.out.println(Colors.RED.ordinal());//

}
}




1 compile time error
2 Runtime Error
3 1 , 2, 0
4
5 None of these

Q 75




class EnumD {
enum DIMENTION {
N("S"), S("E") ,E("N") ,W("W");

String name;
private DIMENTION(String s) {
name = s;
}
public String toString() {
return name;
}
}

public static void main(String [] args) {
System.out.println(DIMENTION.N + " " + DIMENTION.S);

}


}





1 compile time error
2 Runtime Error
3 S E
4
5 None of these
3




Q 76





1 compile time error
2 Runtime Error
3
4
5 None of these

Q 77

enum Device {
DEVEICE1,
DEVICE2,
DEVICE3
}
class DeviceTest{
public static void main(String[] args) {
Device anyDevice = new Device();

}
}





1 compile time error
2 Runtime Error
3
4
5 None of these
1




Q 78





1 compile time error
2 Runtime Error
3
4
5 None of these

Q 79





1 compile time error
2 Runtime Error
3
4
5 None of these




Q 80





1 compile time error
2 Runtime Error
3
4
5 None of these

Q 81





1 compile time error
2 Runtime Error
3
4
5 None of these




Q 82
class Test {
enum Taste {
SWEET { public String toString() { return "very sweet ";} },
HOT { public String toString() { return "very HOT";}},
SPICY { public String toString() { return "very spicy ";}}
}
static class Sample {
enum Taste {
BITTER { public String toString() { return "SWEET-static";}},
SWEET { public String toString() { return "HOT-static";} },
JUICY { public String toString() { return "SPICY-static ";} };
}
}
}
class EnumSix1 {
public static void main(String [] args) {
System.out.println(Test.Taste.SWEET + " " + Test.Sample.Taste.SWEET);
}
}





1 compile time error
2 Runtime Error
3 very sweet , hot static
4
5 None of these

Q 83
enum AccountType {
SAVING, FIXED, CURRENT,FIVEYEAR;


private AccountType() {
System.out.println("It is a account type");
}
}
class EnumOne {
public static void main(String[] args) {
System.out.println(AccountType.FIXED);
}
}





1 compile time error
2 Runtime Error
3 4 times It is a account type , FIXED
4
5 None of these
3




Q 84


class Test {
enum Taste {
SWEET { public String toString() { return "BITTER";} },
HOT { public String toString() { return "HOT";}},
SPICY { public String toString() { return "SWEET";}};
}
static class Sample {
enum Taste {
BITTER { public String toString() { return "SWEET";}},
SWEET { public String toString() { return "HOT";} },
JUICY { public String toString() { return "SPICY";} };
}
}
}
class EnumSix {
public static void main(String [] args) {
//insert the line here


System.out.println(Test.Taste.SWEET);
}
}





1 compile time error
2 Runtime Error
3
4 BITTER
5 None of these
4


Q 85
import java.util.*;
class CollectionTest{
public static void append(List list)
{
list.add(new Integer(1));
}
public static void main(String[] args) {
List intList = new ArrayList();
append(intList);
System.out.println(intList.toString());// will print 1
}
}


1 compile time error
2 Runtime Error
3 [1]
4
5 None of these
3


Q 86

import java.util.*;
class CollectionSearch{
public static void search(List list) {
list.clear();
list.add("b");
list.add("j");
list.add("a");
list.add("c");
list.add("c");
System.out.println(Collections.binarySearch(list, "c"));
}
public static void main(String[] args) {
List l;
search(l);
}}

1 compile time error
2 Runtime Error
3
4
5 None of these
1

Q 87




public class Boxer1{
Integer i;
int x;
public Boxer1(int y) {
x = i+y;
System.out.println(x);
}
public static void main(String[] args) {
new Boxer1(new Integer(4));
}
}


1 compile time error
2 Runtime Error
3
4
5 None of these
2




Q 88

class TestA {
public void start() { System.out.println("TestA"); }

}
class TestB extends TestA {
public void start() { System.out.println("TestB"); }

public static void main(String[] args) {
((TestA)new TestB()).start();
}

}


1 compile time error
2 Runtime Error
3 Test b
4
5 None of these
3


Q 89


public class Ball{
public enum Color { RED, GREEN, BLUE };
public void foo(){
for( Color c : Color.values() )
{
System.out.println(c);
}
}
public static void main(String ar[]){

Ball b= new Ball();
b.foo();
}
}




1 compile time error
2 Runtime Error
3 Red , greern blue
4
5 None of these
3



Q 90

class Outer
{
public Outer()
{
System.out.println("Outer is being instantiated");
}

class Inner{
public Inner()
{
System.out.println("Inner is being instantiated");
}
}
}


public class Other extends Outer.Inner{
public Other(Outer outerRef) {

outerRef.super();

}

public static void main(String... args)
{
Outer outerRef = new Outer();
Other otherRef = new Other(outerRef);
}
}





1 compile time error
2 Runtime Error
3 Outer is being ins, Inner is being instantiet
4
5 None of these
3



Q 91



class EnumTesting{

public enum Title {



MR("Mr."), MRS("Mrs."), MS("Ms.");
private final String title;

private Title(String t) { title = t; }

public String format(String last, String first) {
return title + " " + first + " " + last;
}
}



public static void main(String[] args) {
System.out.println(Title.MR.format("jon", "paul"));
}
}


1 compile time error
2 Runtime Error
3 mr. paul , jon
4
5 None of these
3



Q 92

enum IceCream {
VANILLA ("white"),
STAWBERRY ("pink"),
WALNUT ("brown"),
CHOCOLATE ("dark brown");

String color;

IceCream (String color) {
this.color = color;
}
}

public class enumTest04 {
public static void main (String[] args) {
IceCream vanilla = new IceCream ("white");
System.out.println ( vanilla );
}
}





1 compile time error
2 Runtime Error
3
4
5 None of these
1




Q 93



import java.io.*;

public class Test11 {
//implements Serializable {
public static void main(String[] args) {
Test11.Inner in = new Test11().new Inner();
try{
FileOutputStream fos = new FileOutputStream("Serialize.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(in);
}catch(Exception e){
e.printStackTrace();
}
}
class Inner implements Serializable{
int i =10;
}
}




1 compile time error
2 Runtime Error
3
4
5 None of these
2




Q 94
class Foo {
Foo() {
System.out.print("foo ");
}

class Bar{
Bar()
{System.out.print("bar construoctor ");
}
public void go() {System.out.print(" Bar method go ");}
}
public static void main (String [] args) {
Foo f = new Foo();// foo
f.makeBar();
}
void makeBar() {
(new Bar() {}).go();
}
}




1 compile time error
2 Runtime Error
3 foo , bar construocor , Bar metoid go
4
5 None of these
3

Q 95
interface Foo {
int bar();
}
public class Beta2
{ class A implements Foo
{
public int bar() { return 1; }
}
public int fubar( Foo foo)
{ return foo.bar(); }

public void testFoo()
{

class A implements Foo
{
public int bar()
{ return 2; }
}
System.out.println( "testFoo method in Beta2->"+fubar( new A()));

}


public static void main( String[] argv)
{
new Beta2().testFoo();
}
}

1 compile time error
2 Runtime Error
3 testFoo method in Beta2->
4
5 None of these
3




Q 96


public class InnerClass {
public static void main(String args[]){
System.out.println(doStuff(1));
System.out.println(doStuff(2));
}

public static String doStuff(int y){
final int x=y;
class MyInner{

public String main1(){
return "here "+x;
}
}
MyInner inner = new MyInner();
return inner.main1();

}



}




1 compile time error
2 Runtime Error
3 here 1 ,here 2
4
5 None of these
3




Q 97

interface I {
public class A {// static, final works and wihout static also woriks
int a = 5;
protected void g() {

System.out.println("this is g method");
}
}
}
class Demo1 implements I {
public static void main(String[] args) {

I.A a1 = new I.A( );
a1.g( );
}
}





1 compile time error
2 Runtime Error
3
4 this is g method
5 None of these
4




Q 98

class Foo {
Foo() {System.out.println("foo- outer constroctor ");}
class Bar{
Bar()
{System.out.println("bar construocto r ");
}
public void go() {System.out.println("hi from Bar method go ");}
}
public static void main (String [] args) {
Foo f = new Foo();// foo constoru
f.makeBar();
}
void makeBar() {
(new Bar() {}).go();
}
}





1 compile time error
2 Runtime Error
3 foo outerconstructor , bar constructor , hi fromm bar method go
4
5 None of these
3

Q 99

public class etattva101 {

public static void main(String[] args) {
final int a; // Line 1
final int b; // Line 2
b=10; // Line 3
class MethodLocalClass {

int i =12;
a=19; // Line 4
}
}







1 compile time error in line 4
2 Runtime Error
3
4
5 None of these
1




Q 100

class MainClassForAandB
{

static class A {
void process() throws Exception {
throw new Exception();
}
}

static class B extends A {
void process() { System.out.println("B "); }

}

public static void main(String[] args) {
A a = new B();
a.process();//19


}

}





1 compile time error
2 Runtime Error
3
4
5 None of these
1


===================

2 comments:

  1. 1) 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
    }}
    What is the result of attempting to compile and run the program ?.
    1.compiletime error at lines 3,4,5
    2.compiltime error at line 4,5
    3.compiletime error at line 3
    4.Runtime Exception
    5.None of the above
    Ans:
    Explanation:
    no value shoud be specified in the rightsidebrackets when constructing an array
    2)
    interface I{
    void f1(); // 1
    public void f2(); // 2
    protected void f3(); // 3
    private void f4(); // 4

    }
    which lines generate compile time errors?
    1.compiletime error at lines 1,2,3,4
    2.compiletime error at line 3
    3.compiletime error at line 1
    4.compiletime error at lines 3,4
    5.None of the above
    Answer:
    Explanation:
    all methods declared within an interface are implicitly public, a weaker access level can not be declared.
    3)
    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
    }}
    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
    5.None of the above
    Answer
    Explanation:
    The access modifiers public, protected and private, can not be applied to variables declared inside methods.
    4)
    class C {
    public static void main (String[] a1) {
    System.out.print(a1[1] + a1[2] + a1[3]);
    }}

    What is the result of attempting to compile and run the program?

    java command A B C

    1.Prints: ABC

    2.Prints BC and Runtime Exception

    3.Prints: BCD

    4.Runtime Exception

    5.None of the above

    Answer

    Explanation:

    array index outof bounds exception only till a1[2] is allowed.

    5)

    class C{

    static int s;

    public static void main(String a[]){

    C obj=new C();

    obj.m1();

    System.out.println(s);

    }

    void m1();

    {

    int x=1;

    m2(x);

    System.out.println(x+"");

    }

    void m2(int x){

    x=x*2;

    s=x;

    }}

    1.prints 1,2

    2.prints 2,0

    3.prints 2,2

    4.compile time error

    5.Noneofthe above

    Answer:

    Explanation:

    Only objects and arrays are passed by reference.other are passed by value.s is a static variable which is global to the class

    6)

    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");

    }}}

    What is the result of attempting to compile and run the program?

    1.prints one two three

    2.prints one

    3.compile time error

    4.Runtime exceptionf

    5.None of the above

    Answer:

    Explanation:

    There is no break statement in case 1 so it causes the below case statements to execute regardless of their values

    7)

    Each element must be unique

    Duplicate elements must not replace old elements.

    Elements are not key/value pairs.

    Accessing an element can be almost as fast as performing a similar operation on an array.

    Which of these classes provide the specified features?

    1.LinkedList

    2.TreeMap

    3.HashMap

    4.HashSet

    5.None of the above

    Answer:

    8)

    class C1

    {

    static interface I

    {

    static class C2

    {

    }

    }

    public static void main(String a[])

    {

    C1.I.C2 ob1=new C1.I.C2();

    System.out.println("object created");

    }

    }

    What is the result of attempting to compile and run the program?

    1.prints object created

    2.Compile time error

    3.Runtime Excepion

    4.None of the above

    Answer:

    Explanation:

    A static interface or class can contain static members.Static members can be accessed without instantiating the particular class

    9)

    class C1

    {

    static class C2

    {

    static int i1;

    }

    public static void main(String a[])

    {

    System.out.println(C1.C2.i1);

    }

    }

    What is the result of attempting to compile and run the program?

    1.prints 0

    2.Compile time error

    3.Runtime exception

    4.None of the above

    Answer:

    Explanation:

    static members can be accessed without instantiating the particular class

    10)

    A signed data type has an equal number of non-zero positive and negative values available

    1.true

    2.false

    Answer: 2

    Explanation:

    The range of negative numbers is greater by 1 than the range of positive numbers

    11)

    class C{

    static int f1(int i) {

    System.out.print(i + ",");

    return 0;

    }

    public static void main (String[] args) {

    int i = 0;

    i = i++ + f1(i);

    System.out.print(i);

    }}

    Prints: 0,0

    Prints: 1,0

    Prints: 0,1

    Compile-time error

    None of the above

    Explanation:

    No Explanation Available

    Ans:

    12)

    class C{

    static String m(int i) {return "int";}

    static String m(float i) {return "float";}

    public static void main (String[] args) {

    long a1 = 1; double b1 = 2;

    System.out.print(m(a1)+","+ m(b1));

    }}

    Prints: float,double

    Prints: float,float

    Prints: double,float

    Compile-time error

    None of the above

    Explanation:No Explanation Available

    Ans:

    13)

    class C

    {

    public static void main(String a[])

    {

    C c1=new C();

    C c2=m1(c1);

    C c3=new C();

    c2=c3; //6

    anothermethod();

    }

    static C m1(C ob1){

    ob1 =new C();

    return ob1;

    }

    }

    After line 6, how many objects are eligible for garbage collection?


    Ans: 1

    Ans: 2

    Ans: 3

    Ans: 4

    None of the above

    Explanation:

    No Explanation Available

    Ans:

    14)

    1. StringBuffer s1 = new StringBuffer("abc");

    2. StringBuffer s2 = s1;

    3. StringBuffer s3 = new StringBuffer("abc");

    How many objects are created ?


    0

    Ans: 1

    Ans: 2

    Ans: 3

    Explanation:

    No Explanation Available

    Ans:

    15)

    class c2

    {

    {

    System.out.println("initializer");

    }

    public static void main(String a[])

    {

    System.out.println("main");

    c2 ob1=new c2();

    }

    }


    prints main and initializer

    prints initializer and main

    compile time error

    None of the above

    Explanation:

    No Explanation Available

    Ans:

    16)

    class c1

    {

    public static void main(String a[])

    {

    c1 ob1=new c1();

    Object ob2=ob1;

    System.out.println(ob2 instanceof Object);

    System.out.println(ob2 instanceof c1);

    }

    }


    Prints true,false

    Print false,true

    Prints true,true

    compile time error

    None of the above

    Explanation:

    No Explanation Available

    Ans:

    17)

    class A extends Thread {

    private int i;

    public void run() {i = 1;}

    public static void main(String[] args) {

    A a = new A();

    a.run();

    System.out.print(a.i);

    }}


    Prints nothing

    Prints: 1

    Prints: 01

    Compile-time error

    None of the above

    Explanation:

    a.run() method was called instead of a.start(); so the full program runs as a single thread so a.run() is guaranteed to complete

    Ans:
    18)
    class bike
    {
    }
    class arr extends bike{

    public static void main(String[] args) {

    arr[] a1=new arr[2];
    bike[] a2;
    a2=a1; //3
    arr[] a3;
    a3=a1; //5
    }}
    compile time error at line 3
    compile time error at line 5
    Runtime exception
    The code runs fine
    None of the above
    Explanation:
    bike is the superclass of arr.so they are compatible(superobject=subobject)
    but subobject=superobject not allowed
    Ans:
    19)

    class C{

    public static void main (String[] args) {

    String s1="hjhh"; // 1

    String s2="\u0002"; //2

    String s3="'\\'"; //3

    }}

    compile time error at line 1
    compile time error at line 2
    compile time error at line 3
    Runtime exception
    the code runs without any error
    Explanation:
    A String literal is a sequence of characters enclosed in double quotes
    Ans:
    20)
    Which data type is wider for the purpose of casting: float or long?
    float
    long
    Explanation:
    float is wider than long, because the entire range of long fits within the range of float.

    ReplyDelete
  2. Thanks for the questions. They are awesome.
    Reg Q19, the answer should be #5 as the program compiles and I do see the output.

    ReplyDelete