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;
[Link]("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)
{
[Link](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);
[Link]("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;
[Link](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 < [Link] ; i++){
[Link](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;
}
}
[Link](a[0][0]);
[Link](a[1][3]);
[Link](a[3][4]);
}
}
============================================================================
7) What will be the output of following program?
public class Test {
public static void main(String[] args) {
int i;
[Link](i);
int j=100;
[Link](j);
}
}
============================================================================
8) What will be the output of following program?
public class Test {
int i;
static int j;
public static void main(String[] args) {
[Link](i);
[Link](j);
}
public void non_static(){
[Link](i);
[Link](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(){
[Link]("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(){
[Link]("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(){
[Link]("pass1");
}
public static void meth_static2(){
[Link]("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()
{
[Link]("learning static keyword");
}
}
public class Main
{
public static void main(String s[])
{
Demo ob = new Demo();
[Link]=9;
[Link]();
}
}
============================================================================
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();
[Link]=9;
[Link]("var of ob1 "+[Link]);
Main ob2 = new Main();
[Link]=90;
[Link]("var of ob2 "+[Link]);
[Link]=[Link]+100;
[Link]("ob1 "+[Link]);
[Link]("ob2 "+[Link]);
}
}
============================================================================
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);
[Link](t.i);
}
}
============================================================================
16) What will be the output of following program?
public class Test {
int age;
String name;
Test(int age,String name){
[Link]=age;
[Link]=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;
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
}
}
============================================================================
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(){
[Link]("NM ");
}
public static void static_meth(){
[Link]("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;
[Link](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
[Link](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) {
[Link]( 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;
[Link]( 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
[Link](); // 3
JavaClass t = new JavaClass();
[Link](); // 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
[Link](); //3
[Link](); //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);
[Link](area);
Test t = new Test();
area = calArea2(t);
[Link](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();
[Link](a.x);
[Link](a.y);
[Link](b.x);
[Link](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();
[Link]();
[Link]();
[Link]();
[Link]();
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() {
[Link]("meth1");
@Override
public void meth2() {
[Link]("meth2");
}
@Override
public void meth3() {
[Link]("meth3");
public void meth4() {
[Link]("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){
[Link]([Link]());
[Link]();
}
==============================================================================
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){
[Link]([Link]());
[Link]();
}
[Link](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){
[Link]([Link]());
[Link]();
}
[Link]("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{
[Link]();
[Link]("success");
}catch(Exception e){
[Link]([Link]());
[Link]();
}
}
}
==================================================================================
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);
[Link](a);
int b=divide(4,0);
[Link](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);
[Link](a);
int b=divide(4,0);
[Link](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);
[Link](a);
int b=divide(4,0);
[Link](b);
}
public static int divide(int a,int b) {
int result=0;
try{
result = a/b;
}catch(Exception e){
[Link]();
}
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);
[Link](a);
int b=divide(4,0);
[Link](b);
}catch(Exception e){
[Link]("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);
[Link](a);
int b=divide(4,0);
[Link](b);
}catch(Exception e){
[Link]("error 1");
}
}
public static int divide(int a,int b) {
int result=0;
try{
result=a/b;
}catch(Exception e){
[Link]("error 2");
}
return result;
}
}
===================================================================================
=======
45) What will be the output of following program?
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
[Link]("A");
[Link](5000L);
[Link]("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
[Link]("error 1");
[Link]();
}
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");
}
============================================================================