0% found this document useful (0 votes)
1K views

Class TestQuestion

The document contains 18 code snippets with multiple choice questions about the output of each code snippet when compiled and executed. The code snippets cover topics like loops, conditional statements, data types, operators, and exceptions.

Uploaded by

Jasvan Sundar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Class TestQuestion

The document contains 18 code snippets with multiple choice questions about the output of each code snippet when compiled and executed. The code snippets cover topics like loops, conditional statements, data types, operators, and exceptions.

Uploaded by

Jasvan Sundar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

1)class TestQuestion {

public static void main(String s[ ]) {

for(int p=1 ;p<=2;p++) {

for(int q=1;q<=3;q++) {

if(p==q)

continue;

System.out.print(+p);

System.out.println(+q);

What will be the output when the preceding program is compiled and executed?

A. The program will display the output as:


B. 11
C. 13
D. 11
E. 31

B. The program will generate compilation error.

C. The program will display the output as:

11

12

11

21

D. D. The program will display the output as:

12

13

21

23
2)class TestQuestion {

public static void main(String s[]) {

Boolean a = new Boolean("false");

if(a) {

System.out.println("false");

else {

System.out.println("true");

What will be the output when the preceding program is compiled and executed?

A. The program will display the output as: B. The program will generate compilation error.

False

C. The program will display the output as: D. The program will throw a runtime exception.

True

3)Jennie is a software developer in an MNC. In one of her projects she wrote a program as:

import java.util.*;

class TestQuestion {

public static void main(String args[]) {

String input = "1 fish 2 fish red fish blue fish";

Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");

System.out.println(s.nextInt());

System.out.println(s.nextInt());

System.out.println(s.next());

System.out.println(s.next());

s.close();

}
A. The program will display the output as: . The program will generate compilation error.

red

blue

C. The program will display the output as: D. The program will display the output as:

1 fish

2 fish

fish fish

fish fish

4)Jennie is a software developer in an MNC. In one of her projects, she wrote a program as:

import java.util.*;

class TestQuestion {

public static void main(String args[]) {

Boolean b1 = new Boolean(true);

Boolean b2 = false;

if(b1.equals(b2))

System.out.println("equal");

else

System.out.println("not-equal");

What will be the output when the preceding program is compiled and executed?

A. The program will display the output as: B. The program will generate compilation error.

equal

C. The program will display the output as: D. The program will throw a runtime exception.

not‐equal
5)Jennie is a software developer in an MNC. In one of her projects, she wrote a program as:

class TestQuestion {

public static boolean funcA(int a) {

boolean b = (a==10)?true:false;

System.out.println(b);

return b;

public static void main(String args[]) {

if(funcA(10) && funcA(5))

System.out.println("pass");

What will be the output when the preceding program is compiled and executed?

A. The program will display the output as: B. The program will generate compilation error.

true

false

C. The program will display the output as: D. The program will throw a runtime exception.

Pass

6)Jaya is a software developer in an MNC. In one of her projects she wrote a program as:

class TestQuestion {

static char x, y;//static

public static void main(String s[]) {

int sum = x+y;

System.out.println(sum);

During compilation and execution of the preceding program, what will happen?

A. The program will compile and execute successfully, but no output will be shown.
B. The program will generate compilation error.

C. The program will display the output as: 0

D. The program will throw a runtime exception.

7)Hari is a software developer in an MNC. In one of his projects, he wrote a program as:

class TestQuestion {

public static void main(String s[]) {

int num1 = 0102;//66

int num2 = -10;

int operation = num1 % num2;

System.out.println(+operation);

What will be the output when the preceding program is compiled and executed?

A. The program will display the output as:

B. The program will generate compilation error.

C. The program will display the output as:

‐6

D. The program will throw a runtime exception.

8)Maya is a software developer in an MNC. In one of her projects she wrote a program as:

class TestQuestion {

public static void main(String s[]) {

System.out.println(s[0] + s[1]);

}
What will be the output when the preceding program is run with the following command line argument:

>java TestQuestion 12 24

A. The program will display the output as: B. The program will generate compilation error.

36

C. The program will display the output as: D. The program will throw a runtime exception.

1224

9)Jyoti is a software developer in an MNC. In one of her projects she wrote a program as:

public class TestQuestion{

public static void main(String[] args) {

String str = "PQR";

int len = str.length();

switch (len) {

case 1: System.out.println("January"); break;

case 2: System.out.println("February"); break;

case 3: System.out.println("March"); break;

default: System.out.println("Invalid month.");break;

What will be the output when the preceding program is compiled and executed?

A. The program will display the output as: B. The program will generate compilation error.

March

C. The program will display the output as: D. The program will throw a runtime exception.

January

10)Uma is a student of computer science and has written a program as:

public class TestQuestion {

public static void main(String s[]) {

boolean a;
boolean p = false;

boolean q = false;

if(a = p == q) {

System.out.println("equal");

else

System.out.println("not-equal");

What will be the output when the preceding code is compiled and executed?

A. The program will display the output as: B. The program will generate compilation error.

equal

C. The program will display the output as: D. The program will throw a runtime exception.

not‐equal

11)Imagine you are a software developer in ABC. Company and write the following program for your

project:

class BooleanRep {

public static void main(String ar[]) {

boolean status=1; // this statement will generate error

if(status) {

System.out.println("Here Status= "+status);

What will happen when you compile and exceute the preceeding program?

A. The program generates compile time error.

B. The program compiles successfully and displays nothing as output.

C. The program compiles successfully but generates runtime exception.


D. The program compiles successfully and displays Here Status= 1 as output.

12)Imagine during your preparation of the SCJP exam, you came across the following program:

class AssignRep {

public static void main(String ar[]) {

short b1=2;

short b2=3;

short b3=b1+b2; // error in this statement

System.out.println("Result= "+b3);

What will happen when you compile and exceute the preceeding program?

A. The program generates compile time error.

B. The program compiles successfully and displays nothing as output.

C. The program compiles successfully but generates runtime exception.

D. The program compiles successfully and displays Result= 5 as output.

13)Dheeraj works as a Programmer in Kogent Solutions Inc. He writes the following program:

class MyQuestion {

public static void main(String args[]) {

int a = 10;

boolean bool1 = false;

boolean bool2 = true;

if ((a == 7) && !bool1)

System.out.print("Welcome ");

System.out.print("to ");

if (( bool1 = true) && bool2)


System.out.print("Java ");

What will happen when he tries to compile and execute the preceding program?

A. The program will display: Welcome B. The program will display: to Java

C. The program will display: Welcome to D. The program will generate compilation error

14)Ria works as a Programmer for TimeNet Inc. She writes the following program:

public class Test {

static public void main(String rat[]) {

for(int z=2 ; z<rat.length;z++) {

System.out.print(rat[z] + " ");

What will happen if she tries to execute the preceding program by writing the following command line

argument?

java Test 456

A. It will display the output as: B. It will display the output as:

456 6

C. It will throw a runtime exception. D. It will compile and execute but does not

display anything as output.

15)Imagine you work as a programmer in the ABC Company. You write the following program:

public class A {

public enum Cars {ferari, maruti, omni};

public static void main(String [] args) {

Cars mycar = Cars.ferari;


switch (mycar) {

case ferari:

System.out.print("ferari ");

default case:

System.out.print("mercedez ");

case maruti:

System.out.print("maruti ");

What will happen when you try to compile and execute the preceding program?

A. It will produce the output as: B. It will produce the output as:

Feraari mercedez

C. It will produce the output as: D. It will generate compilation error.

true false done

16)Neha works as a programmer in the ABC Company and she has written the following program:

public class Foo {

public static void main(String s[]) {

Integer i = new Integer(1) + new Integer(2);

switch(i) {

case 3: System.out.println("three"); break;

default: System.out.println("other"); break;

What will be the output when the preceding programis compiled and executed?

A. two B. three C. other D. program will not compile successfully.


17)Peter works as a programmer in the ABC Company and has written the following program:

public class MyClass {

public static void main(String s[]) {

int x= 0;

int y = 10;

do {

y--;

++x;

} while (x < 5);

System.out.print(x + "," + y);

What will be the output when the preceding program is compiled and executed?

A. zero B. sum C. other D. Program will not compile successfully.

18)Imagine you work as a programmer in the ABC Company and you have written the following
program:

class Next {

public static void main(String s[]) {

int x=12;

while (x < 10) {

x--;

System.out.print(x);

}}

What will be the output when the preceding program is compiled and executed?

A. 11 B. 12 C. 23 D. Program will not compile successfully.


19)Imagine you work as a programmer in the ABC Company and you have written the following
program:

class Test {

public static void main(String s[]) {

int x = 10;

do {

System.out.println(x--);

while(x>10);

How many times will the value of x be printed after compilation and execution of the preceding

program?

A. 10 B. 1 C. 2 D. Program will not compile successfully.

20)Imagine you work as a programmer in the ABC Company and you have written the following
program:

class Test {

public static void main(String[] args) {

for (int i = 0;i <=10;i++){

if( i > 6) break;

System.out.println(i);

What will be the output after compilation and execution of the preceding program?

A. 1 2 3 B. 1 2 3 4 C. 4 4 D. Program will not compile successfully.

21)Imagine you work as a programmer in the ABC Company and you have written the following
program:

class Testing {

public static void main(String s[]) {


Float pi = new Float(3.14f);

if(pi>3) {

System.out.print("pi is big");

else {

System.out.print("pi is not big");

finally {

System.out.println("Have a nice day.");

What will be the output after compilation and execution of the preceding program?

A. pi is big B. pi is not big C. Have a nice day D. Program will not compile successfully.

22)class FloatingPointRep {

public static void main(String ar[]) {

float height=6.45; // this statement will generate error

System.out.println("Height= "+height);

What will happen when you compile and exceute the preceeding program?

A. The program generates compile time error.

B. The program compiles successfully and displays nothing as output.

C. The program compiles successfully but generates runtime exception.

D. The program compiles successfully and displays Height = 6.45 as output.

You might also like