Please do not copy paste code in eclipse. Try it yourself with pen and paper !!
===============================================================================
1) What will following lines print:
String x="We are learning";
String y="mistakes happen";
int z=1000;
System.out.println("Java is easy. "+ x + " selenium and "+ y +" "+z +" times")
============================================================================
2) What will be the output of following program
public class Test {
public static void main(String[] args) {
int i=2;
f1(1);
public static void f1(int i)
{
f2(i+1);
}
public static void f2(int i)
{
f3(i+2);
}
public static void f3(int i)
{
System.out.println(i+3);
}
}
============================================================================
3) What will be the output of following:
public class Test {
public static void main(String[] args) {
int x=0;
while(true){
x = increment(x);
System.out.println("Value of x is --"+x);
if(x>10)
break;
}
}
public static int increment(int i){
return i+1;
}
}
============================================================================
4) What will be the output of following program
public class Test {
public static void main(String[] args) {
int i=2;
while(makeDecision(i)){
i=i*i;
System.out.println(i);
}
public static boolean makeDecision(int i)
{
if(i%3 != 0){
return true;
}else{
return false;
}
}
}
============================================================================
5) What will be the output of following:
public class Test {
public static void main(String[] args) {
String arr1[] = new String [3];
String arr2[] = new String [3];
arr1[0]="A";
arr1[1]="B";
arr1[2]="C";
arr2[0]="1";
arr2[1]="2";
arr2[2]="3";
printAll(arr2);
printAll(arr1);
public static void printAll(String str[]){
for(int i=0; i < str.length ; i++){
System.out.println(str[i]);
}
}
}
============================================================================
6) What will be the output of following:
public class Test {
public static void main(String[] args) {
int a[][] = new int[10][5];
for(int i=0;i<10;i++){
for(int j=0; j<5; j++){
a[i][j]=i*j;
}
}
System.out.println(a[0][0]);
System.out.println(a[1][3]);
System.out.println(a[3][4]);
}
}
============================================================================
7) What will be the output of following program?
public class Test {
public static void main(String[] args) {
int i;
System.out.println(i);
int j=100;
System.out.println(j);
}
}
============================================================================
8) What will be the output of following program?
public class Test {
int i;
static int j;
public static void main(String[] args) {
System.out.println(i);
System.out.println(j);
}
public void non_static(){
System.out.println(i);
System.out.println(j);
}
}
============================================================================
9) What will be the output of following program?
public class Test {
public static void main(String[] args) {
non_static();
}
public void non_static(){
System.out.println("pass");
}
============================================================================
10) What will be the output of following program?
public class Test {
int i;
static int j;
public static void main(String[] args) {
non_static();
}
public static void non_static(){
System.out.println("pass");
}
============================================================================
11) What will be the output of following program?
public class Test {
int i;
static int j;
public static void main(String[] args) {
Test t = new Test();
t.non_static();
t.meth_static2();
meth_static2();
t.i=100;
j=200;
t.j=400;
}
public void non_static(){
System.out.println("pass1");
}
public static void meth_static2(){
System.out.println("pass1");
}
}
============================================================================
12) Will this code compile?
public class Demo1 {
int var=10;
public static void main(String s[])
{
int local=var;
}
}
============================================================================
13) Will this code compile?
class Demo
{
static int var=9;
public static void func()
{
System.out.println("learning static keyword");
}
}
public class Main
{
public static void main(String s[])
{
Demo ob = new Demo();
ob.var=9;
ob.func();
}
}
============================================================================
14) What will be the output of following program?
public class Main
{
int var;
static int stc=7;
public static void main(String s[])
{
Main ob1 = new Main();
ob1.var=9;
System.out.println("var of ob1 "+ob1.var);
Main ob2 = new Main();
ob2.var=90;
System.out.println("var of ob2 "+ob2.var);
ob1.stc=ob1.stc+100;
System.out.println("ob1 "+ob1.stc);
System.out.println("ob2 "+ob2.stc);
}
}
============================================================================
15) What will be the output of following program?
public class Test {
int i;
Test(int i){
i=i;
}
public static void main(String[] args) {
Test t = new Test(7);
System.out.println(t.i);
}
}
============================================================================
16) What will be the output of following program?
public class Test {
int age;
String name;
Test(int age,String name){
this.age=age;
this.name=name;
}
public static void main(String[] args) {
Test t1 = new Test(17,"A");
Test t2 = new Test(13,"B");
Test t3 = new Test(14,"C");
t3=t2;
t2=t1;
t1=t3;
System.out.print(t1.age);
System.out.print(t2.age);
System.out.println(t3.age);
}
}
============================================================================
17) Whats the output of following program?
public class Test {
int age;
String name;
Test(){
non_static_meth();
static_meth();
}
public static void main(String[] args) {
Test t1 = new Test();
}
public void non_static_meth(){
System.out.print("NM ");
}
public static void static_meth(){
System.out.println("SM");
}
============================================================================
18) In real world, Contructors are used to:
1) Initialize all variables of a class
2) Initialize non-static varialbles of a class
3) static variables can be initialized in constructors
4) Give initial state to object
============================================================================
19) Whats the output of following program?
public class Test {
int i;
int j;
Test (int i, int j){
this.i=i;
this.j=j;
}
public static void main(String[] args) {
Test t1 = new Test();
Test t2 = new Test();
}
============================================================================
20) Whats the output of following program?
public class Test {
int i;
int j;
public static void main(String[] args) {
Test t1 = new Test();
Test t2 = new Test();
t1.j=t2.i=5;
t1.i=t2.j=6;
System.out.print(t1.j++ + " " + t2.i--);
}
}
============================================================================
21) Whats the output of following program?
public class Test {
Test t1= new Test();
int i;
static int j;
static Test t2 = new Test();
public static void main(String[] args) {
t1.i=10; //1
i=19; //2
j=10; //3
t2.i=19; //4
}
}
============================================================================
22) Compile-time errors are generated at which lines?
public class Test {
public static void main(String[] args) {
public int a; // 1
protected int b; // 2
private int c; // 3
static int d; // 4
transient int e; // 5
volatile int f; // 6
final int g = 1; // 7
int i=7; // 8
int h; //9
System.out.println(h); //10
}
}
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10
============================================================================
23) What will be output of follwoing?
class JavaClass {
static int i;
static JavaClass obj;
public static void main (String[] args) {
System.out.println( obj + "" +i);
}}
============================================================================
24) What will be output of follwoing?
public class Test {
static int i;
static Test obj;
public static void main (String[] args) {
Test obj;
int i;
System.out.println( obj + "" +i);
}}
============================================================================
25) A compile-time error is generated at which line?
public class Training {
public static void main(String[] args) {
static int a=1; //1
int b=1; //2
}
public void abc(){
static int a=1; //3
int b=1; //4
}
============================================================================
26) Which is the valid way of calling the main1 method?
public class JavaClass {
public static void main(String[] arg){
main1(); //1
JavaClass j = new JavaClass();
j.main1(); //2
}
public void main1(){
a) 1
b) 2
c) Both 1 and 2
d) Neither 1 nor 2
e) None of these
============================================================================
27) Compile time errors are generated at which lines?
public class JavaClass {
int i=1;
static int a=1;
public static void main(String[] args) {
public void nonstaticMethod(){
calArea(); // 1
nonstaticMethod(); //2
JavaClass.calArea(); // 3
JavaClass t = new JavaClass();
t.calArea(); // 4
i=i+1; // 5
a=a+1; // 6
static int b=1; // 7
}
public static int calArea(){
return 8*8;
}
}
a) 1,2,5,7
b) 2,5,7
c) 7
d) 2,4,6,7
e) 4,5,7
============================================================================
28) Compile time errors are generated at which lines?
public class JavaClass {
int i=1;
static int a=1;
public static void main(String[] args) {
JavaClass t= new JavaClass();
calArea(); //1
nonstaticMethod(); //2
JavaClass.calArea(); //3
t.calArea(); //4
i=i+1; //5
a=a+1; //6
static int b=1; //7
public void nonstaticMethod(){
public static int calArea(){
return 1*1;
}
}
a) 1,2,5,7
b) 2,5,7
c) 4,6,7
d) 2,4,6,7
e) 4,5,7
============================================================================
29) What will be outut of following program?
public class Test {
int i;
int j;
public static void main(String[] args) {
int area = calArea1(3,4);
System.out.println(area);
Test t = new Test();
area = calArea2(t);
System.out.println(area);
}
public static int calArea1(int i, int j) {
return i*j;
}
public static int calArea2(Test t) {
t.i=t.i+10;
t.j=t.i+20;
return t.i*t.j;
}
}
============================================================================
30) What will be the output of following?
public class Test {
public static void main(String[] args) {
A a = new A();
B b = new B();
System.out.println(a.x);
System.out.println(a.y);
System.out.println(b.x);
System.out.println(b.y);
class A{
String x="Parent";
}
class B extends A{
String y="Child";
}
============================================================================
31) What will be the output of following?
public class Test {
public static void main(String[] args) {
A a = new A();
B b = new B();
a.parentMeth();
a.childMeth();
b.childMeth();
a.parentMeth();
class A{
public void parentMeth(){
}
}
class B extends A{
public void childMeth(){
}
============================================================================
32) What will be the output of following?
class A{
}
class B {
class C extends A,B{
}
============================================================================
33) What will be the output of following?
interface A{
interface B {
class C implements A,B{
}
============================================================================
34) What will be the output of following?
public class Test{
public static void main(String[] a){
A a1 = new B();
a1.meth1();
a1.meth2();
a1.meth3();
a1.meth4();
}
interface A{
public void meth1();
public void meth2();
public void meth3();
}
class B implements A{
@Override
public void meth1() {
System.out.println("meth1");
@Override
public void meth2() {
System.out.println("meth2");
}
@Override
public void meth3() {
System.out.println("meth3");
public void meth4() {
System.out.println("meth4");
}
============================================================================
35) What will be the output of following program?
public class Test {
public static void main(String[] args) {
try{
int o[] = new int[2];
o[3]=23;
}catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
==============================================================================
36) What will be the output of following program?
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
try{
int o[] = new int[2];
o[3]=23;
o[1]=33;
}catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
System.out.println(o[1]);
}
===============================================================================
37) What will be the output of following program?
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
int o[] = new int[2];
try{
o[3]=23;
o[1]=33;
}catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
System.out.println("2nd pos --"+o[1]);
}
}
================================================================================
38) What will be the output of following program?
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
SomeClass obj=null;
try{
obj.someMethod();
System.out.println("success");
}catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
==================================================================================
39) What will be the output of following program?
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
divide(4,2);
divide(4,0);
}
public static int divide(int a,int b) throws Exception{
int result = a/b;
return result;
}
}
===================================================================================
40) What will be the output of following program?
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
int a=divide(4,2);
System.out.println(a);
int b=divide(4,0);
System.out.println(b);
}
public static int divide(int a,int b) throws Exception{
int result = a/b;
return result;
}
}
===================================================================================
==
41) What will be the output of following program?
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
int a=divide(4,2);
System.out.println(a);
int b=divide(4,0);
System.out.println(b);
}
public static int divide(int a,int b) {
int result = a/b;
return result;
}
}
===================================================================================
=====
42) What will be the output of following program?
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
int a=divide(4,2);
System.out.println(a);
int b=divide(4,0);
System.out.println(b);
}
public static int divide(int a,int b) {
int result=0;
try{
result = a/b;
}catch(Exception e){
e.printStackTrace();
}
return result;
}
}
===================================================================================
======
43) What will be the output of following program?
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
try{
int a=divide(4,2);
System.out.println(a);
int b=divide(4,0);
System.out.println(b);
}catch(Exception e){
System.out.println("error");
}
}
public static int divide(int a,int b) {
int result=a/b;
return result;
}
}
===================================================================================
======
44) What will be the output of following program?
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
try{
int a=divide(4,2);
System.out.println(a);
int b=divide(4,0);
System.out.println(b);
}catch(Exception e){
System.out.println("error 1");
}
}
public static int divide(int a,int b) {
int result=0;
try{
result=a/b;
}catch(Exception e){
System.out.println("error 2");
}
return result;
}
}
===================================================================================
=======
45) What will be the output of following program?
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("A");
Thread.sleep(5000L);
System.out.println("B");
}
==========================================================================
46) What will be the output of following program?
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
throw new Exception("Some exception");
}
=========================================================================
47) What will be the output of following program?
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
xyz();
public static void xyz() throws Exception{
throw new Exception("Some exception");
}
===========================================================================
48) What will be the output of following program?
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
try {
xyz();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("error 1");
e.printStackTrace();
}
public static void xyz() throws Exception{
throw new Exception("Some exception");
}
==========================================================================
49) What will be the output of following program?
public class Test {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
xyz();
}
public static void xyz() throws Exception{
throw new Exception("Some exception");
}
============================================================================