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);
}
}
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
}
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) 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.
1) Class C {
ReplyDeletepublic 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.
Thanks for the questions. They are awesome.
ReplyDeleteReg Q19, the answer should be #5 as the program compiles and I do see the output.