0% found this document useful (0 votes)
546 views61 pages

Size of Int in Java Is: A. 16 Bit B. 32 Bit C. 64 Bit D. Depends On Execution Environment Answer: Option B

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 61

1. Java is a ........... language.

A. weakly typed B. strongly typed C. moderate typed D. None of these

Answer: Option B

2. How many primitive data types are there in Java?

A. 6 B. 7 C. 8 D. 9

Answer: Option C

3. In Java byte, short, int and long all of these are

A. signed B. unsigned C. Both of the above D. None of these

Answer: Option A

4. Size of int in Java is

A. 16 bit B. 32 bit C. 64 bit D. Depends on execution environment

Answer: Option B

5. The smallest integer type is ......... and its size is ......... bits.

A. short, 8 B. byte, 8 C. short, 16 D. short, 16

Answer: Option B

6. Size of float and double in Java is

A. 32 and 64 B. 64 and 64 C. 32 and 32 D. 64 and 32

Answer: Option A

7. Determine output:

class A{
public static void main(String args[]){
int x;
x = 10;
if(x == 10){
int y = 20;
System.out.print("x and y: "+ x + " " + y);
y = x*2;
}
y = 100;
System.out.print("x and y: " + x + " " + y);
}
}
A. 10 20 10 100 B. 10 20 10 20

C. 10 20 10 10 D. Error

Answer: Option D

8. Automatic type conversion in Java takes place when

A. Two type are compatible and size of destination type is shorter than source type.

B. Two type are compatible and size of destination type is equal of source type.

C. Two type are compatible and size of destination type is larger than source type.

D. All of the above

Answer: Option C

9. Which of the following automatic type conversion will be possible?

A. short to int B. byte to int

C. int to long D. long to int

Answer: Option C

10. What is the output of the following program?

class A{
public static void main(String args[]){
byte b;
int i = 258;
double d = 325.59;

b = (byte) i;
System.out.print(b);

i = (int) d;
System.out.print(i);

b = (byte) d;
System.out.print(b);
}
}

A. 258 325 325 B. 258 326 326

C. 2 325 69 D. Error

Answer: Option C

11. Determine output:

public class Test {


static void test(float x){
System.out.print("float");
}

static void test(double x){


System.out.print("double");
}

public static void main(String[] args){


test(99.9);
}
}

A. float B. double

C. Compilation Error D. Exception is thrown at runtime

Answer: Option B

12. The following fraction of code

double STATIC = 2.5 ;


System.out.println( STATIC );

A. Prints 2.5

B. Rraises an error as STATIC is used as a variable which is a keyword

C. Raises an exception D. None of these

Answer: Option A

13. What would be the output of the following fraction of code ?

int Integer = 34 ;
char String = 'S' ;
System.out.print( Integer ) ;
System.out.print( String ) ;

A. Does not compile as Integer and String are API class names.

B. Throws exception. C. 34 D. S E. 34 S

Answer: Option E

14. What is the output of the following program?

public class Test{


static int x = 10 ;
public static void main(String[] a){
Test test = new Test( ) ;
Test test1 = new Test( ) ;
test.x += 1 ;
System.out.println( test.x + test1.x ) ;
}
}

A. 20 B. 21 C. 22 D. Compilation Error
E. Throws Exception

Answer: Option C

15. What will be output of the following program code?

public class Test{


public static void main(String[] a){
short x = 10;
x = x*5;
System.out.print(x);
}
}

A. 50 B. 10 C. Compilation Error D. None of these

Answer: Option C

16. Determine output:

public class Test{


int i = 34;
public static void main(String args[]){
Test t1 = new Test();
Test t2 = new Test();
t1.i = 65;
System.out.print(t1.i);
System.out.print(t2.i);
}
}

A. 34 34 B. 65 34 C. 65 65 D. 34 65

Answer: Option B

17. The following program:

public class Test{


static boolean isOK;
public static void main(String args[]){
System.out.print(isOK);
}
}

A. Prints true B. Prints false

C. Will not compile as boolean is not initialized

D. Will not compile as boolean can never be static

Answer: Option B

18. In Java, the word true is ................

A. A Java keyword B. A Boolean literal

C. Same as value 1 D. Same as value 0


Answer: Option B

19. What will the output of the following program?

public class Test{


public static void main(String args[]){
float f = (1 / 4) * 10;
int i = Math.round(f);
System.out.println(i);
}
}

A. 2 B. 0 C. 3

D. 2.5 E. 25

Answer: Option B

20. What is the output for the below code ?

class A{
int k;
boolean istrue;
static int p;
public void printValue(){
System.out.print(k);
System.out.print(istrue);
System.out.print(p);
}
}

public class Test{


public static void main(String argv[]){
A a = new A();
a.printValue();
}
}

A. 0 false 0 B. 0 true 0 C. 000

D. Compile error - static variable must be initialized before use. E. None of these

Answer: Option A

21. What is the output for the below code?

public class Test{


int _$;
int $7;
int do;

public static void main(String argv[]){


Test test = new Test();
test.$7=7;
test.do=9;
System.out.println(test.$7);
System.out.println(test.do);
System.out.println(test._$);
}
}
A. 790 B. 700

C. Compile error - $7 is not valid identifier.

D. Compile error - do is not valid identifier. E. None of these

Answer: Option D

22. What is the output for the below code ?

1. public class Test{


2. public static void main(String[] args){
3. int i = 010;
4. int j = 07;
5. System.out.println(i);
6. System.out.println(j);
7. }
8. }

A. 87 B. 10 7

C. Compilation fails with an error at line 3

D. Compilation fails with an error at line 5 E. None of these

Answer: Option A

23. What is the output for the below code ?

1. public class Test{


2. public static void main(String[] args){
3. byte b = 6;
4. b+=8;
5. System.out.println(b);
6. b = b+7;
7. System.out.println(b);
8. }
9. }

A. 14 21 B. 14 13

C. Compilation fails with an error at line 6

D. Compilation fails with an error at line 4 E. None of these

Answer: Option C

24. What will be the output for the below code ?

1. public class Test{


2. public static void main(String[] args){
3. byte i = 128;
4. System.out.println(i);
5. }
6. }
A. 128 B. 0 C. Compilation fails with an error at line
3

D. Compilation fails with an error at line 4 E. None of these

Answer: Option C

25. What will be the output for the below code ?

1. public class Test{


2. int i=8;
3. int j=9;
4. public static void main(String[] args){
5. add();
6. }
7. public static void add(){
8. int k = i+j;
9. System.out.println(k);
10. }
11. }

A. 17 B. 0 C. Compilation fails with an error at line 5

D. Compilation fails with an error at line 8 E. None of these

Answer: Option D

26. What will be output of following program?

public class Test{


public static void main(String[] args){
byte b=127;
b++;
b++;
System.out.println(b);
}
}

A. 2 B. 129 C. -127 D. Compiler error

E. None of these

Answer: Option C

27. Determine output:

public class Test{


int a = 10;

public void method(int a){


a += 1;
System.out.println(++a);
}
public static void main(String args[]){
Test t = new Test();
t.method(3);
}
}
A. 4 B. 5 C. 12 D. 11

E. None of these

Answer: Option B

1. In Java arrays are

A. objects B. object references C. primitive data type

D. None of the above

Answer: Option A

2. Which one of the following is a valid statement?

A. char[] c = new char(); B. char[] c = new char[5];

C. char[] c = new char(4); D. char[] c = new char[];

Answer: Option B

3. What is the result of compiling and running the following code?

public class Test{


public static void main(String[] args){
int[] a = new int[0];
System.out.print(a.length);
}
}

A. 0

B. Compilation error, arrays cannot be initialized to zero size.

C. Compilation error, it is a.length() not a.length

D. None of the above

Answer: Option A

4. What will be the output?

public class Test{


public static void main(String[] args){
int[] x = new int[3];
System.out.println("x[0] is " + x[0]);
}
}

A. The program has a compile error because the size of the array wasn't specified when
declaring the array.
B. The program has a runtime error because the array elements are not initialized.

C. The program runs fine and displays x[0] is 0.

D. The program has a runtime error because the array element x[0] is not defined.

Answer: Option C

5. What is the output of the following code?

public class Test{


public static void main(String args[]){
double[] myList = {1, 5, 5, 5, 5, 1};
double max = myList[0];
int indexOfMax = 0;
for(int i = 1; i < myList.length; i++){
if(myList[i] > max){
max = myList[i];
indexOfMax = i;
}
}
System.out.println(indexOfMax);
}
}

A. 0 B. 1

C. 2 D. 3 E. 4

Answer: Option B

6. What is output of the following code:

public class Test{


public static void main(String[] args){
int[] x = {120, 200, 016 };
for(int i = 0; i < x.length; i++)
System.out.print(x[i] + " ");
}
}

A. 120 200 16 B. 120 200 14

C. 120 200 016 D. 016 is a compile error. It should be written as 16.

Answer: Option B

7. Determine output:

public class Test{


public static void main(String[] args){
int[] x = {1, 2, 3, 4};
int[] y = x;

x = new int[2];

for(int i = 0; i < x.length; i++)


System.out.print(y[i] + " ");
}
}

A. 1234 B. 0000

C. 12 D. 00 E. None of these

Answer: Option C

8. Analyze the following code and choose the correct answer.

int[] arr = new int[5];


arr = new int[6];

A. The code has compile errors because the variable arr cannot be changed once it is
assigned.

B. The code has runtime errors because the variable arr cannot be changed once it is
assigned.

C. The code can compile and run fine. The second line assigns a new array to arr.

D. The code has compile errors because we cannot assign a different size array to arr.

Answer: Option C

9. What will be the output?

public class Test{


public static void main(String[] args){
int[] a = new int[4];
a[1] = 1;
a = new int[2];
System.out.println("a[1] is " + a[1]);
}
}

A. The program has a compile error because new int[2<sp< label=""> </sp<>

B. The program has a runtime error because a[1 C. a[1] is 0


D. a[1] is 1

Answer: Option C

10. When you pass an array to a method, the method receives ________ .

A. A copy of the array. B. A copy of the first element.

C. The reference of the array. D. The length of the array.

Answer: Option C

11. What would be the result of attempting to compile and run the following code?
public class HelloWorld{
public static void main(String[] args){
double[] x = new double[]{1, 2, 3};
System.out.println("Value is " + x[1]);
}
}

A. The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it
should be replaced by {1, 2, 3}.

B. The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it
should be replaced by new double[3]{1, 2, 3};

C. The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it
should be replaced by new double[]{1.0, 2.0, 3.0};

D. The program compiles and runs fine and the output

Answer: Option D

12. Which will legally declare, construct, and initialize an array?

A. int [] myList = {}; B. int [] myList = (5, 8, 2);

C. int myList [] [] = {4,9,7,0}; D. int myList [] = {4, 3, 7};

Answer: Option D

13. What will be the output of the program?

public class Test{


public static void main(String [] args){
String s1 = args[1];
String s2 = args[2];
String s3 = args[3];
String s4 = args[4];
System.out.print(" args[2] = " + s2);
}
}

and the command-line invocation is C:Java> java Test 1 2 3 4

A. args[2] = 2 B. args[2] = 3 C. args[2] = null

D. An exception is thrown at runtime.

Answer: Option D

14. What is the value of a[1] after the following code is executed?

int[] a = {0, 2, 4, 1, 3};


for(int i = 0; i < a.length; i++)
a[i] = a[(a[i] + 3) % a.length];

A. 0 B. 1 C. 2
D. 3 E. 4

Answer: Option B

1. The output of the following fraction of code is

public class Test{


public static void main(String args[]){
String s1 = new String("Hello");
String s2 = new String("Hellow");
System.out.println(s1 = s2);
}
}

A. Hello B. Hellow C. Compilation error

D. Throws an exception E. None of these

Answer: Option B

2. What will be the output of the following program code?

class LogicalCompare{
public static void main(String args[]){
String str1 = new String("OKAY");
String str2 = new String(str1);
System.out.println(str1 == str2);
}
}

A. true B. false C. 0

D. 1 E. Displays error message

Answer: Option B

3. What will be the output of the following program?

public class Test{


public static void main(String args[]){
String s1 = "java";
String s2 = "java";
System.out.println(s1.equals(s2));
System.out.println(s1 == s2);
}
}

A. false true B. false false C. true false

D. true true

Answer: Option D

4. Determine output:

public class Test{


public static void main(String args[]){
String s1 = "SITHA";
String s2 = "RAMA";
System.out.println(s1.charAt(0) > s2.charAt(0));
}
}

A. true B. false C. 0

D. Compilation error E. Throws Exception

Answer: Option A

5. What could be output of the following fragment of code?

public class Test{


public static void main(String args[]){
String x = "hellow";
int y = 9;
System.out.println(x += y);
}
}

A. Throws an exception as string and int are not compatible for addition

B. hellow9 C. 9hellow

D. Compilation error E. None of these

Answer: Option B

6. toString() method is defined in

A. java.lang.String B. java.lang.Object

C. java.lang.util D. None of these

Answer: Option B

7. The String method compareTo() returns

A. true B. false C. an int value

D. 1 E. -1

Answer: Option C

8. What will be the output?

String str1 = "abcde";


System.out.println(str1.substring(1, 3));

A. abc B. bc C. bcd
D. abcd E. None of these

Answer: Option B

9. What is the output of the following println statement?

String str1 = "Hellow";


System.out.println(str1.indexOf('t'));

A. true B. false C. 1

D. -1 E. 0

Answer: Option D

10. What will be the output of the following program?

public class Test{


public static void main(String args[]){
String str1 = "one";
String str2 = "two";
System.out.println(str1.concat(str2));
}
}

A. one B. two C. onetwo

D. twoone E. None of these

Answer: Option C

11. String str1 = "Kolkata".replace('k', 'a');

In the above statement, the effect on string Kolkata is

A. The first occurrence of k is replaced by a. B. All characters k are replaced by a.

C. All characters a are replaced by k. D. Displays error message

Answer: Option B

12. The class string belongs to ................. package.

A. java.awt B. java.lang C. java.applet

D. java.string

Answer: Option B

13. What will be the output?

public class Test{


public static void main (String[] args){
String test = "a1b2c3";
String[] tokens = test.split("\\d");
for(String s: tokens)
System.out.print(s);
}
}

A. abc B. 123 C. Runtime exception thrown

D. Compilation error

Answer: Option A

14. How many objects will be created?

String a = new String("Examveda");


String b = new String("Examveda");
String c = "Examveda";
String d = "Examveda";

A. 4 B. 3 C. 2 D. None of this

Answer: Option B

15. How many Constructor String class have?

A. 2 B. 7 C. 9 D. 11

E. None of this

Answer: Option D

16. What will be output?

String S1 = "S1 ="+ "123"+"456";


String S2 = "S2 ="+(123+456);

A. S1=123456, S2=579 B. S1=123456,S2=123456

C. S1=579,S2=579 D. None of This

Answer: Option A

17. What will be the output of the following program code?

public class Test{


public static void main(String args[]){
String s = "what";
StringBuffer sb = new StringBuffer("what");
System.out.print(sb.equals(s)+","+s.equals(sb));
}
}

A. true,true B. false,true

C. true,false D. false,false E. None of these


Answer: Option D

18. What will be the output?

1. public class Test{


2. public static void main(String args[]){
3. Object myObj = new String[]{"one", "two", "three"};
4. {
5. for(String s : (String[])myObj)
6. System.out.print(s + ".");
7. }
8. }
9. }

A. one.two.three. B. Compilation fails because of an error at line 3

C. Compilation fails because of an error at line 5

D. An exception is thrown at runtime. E. None of these

Answer: Option A

19. Determine output:

public class Test{


public static void main(String args[]){
String str = null;
if(str.length() == 0){
System.out.print("1");
}
else if(str == null){
System.out.print("2");
}
else{
System.out.print("3");
}
}
}

A. Compilation fails. B. "1" is printed.

C. "2" is printed. D. "3" is printed.

E. An exception is thrown at runtime.

Answer: Option E

1. Determine output:

public class Test{


public static void main(String args[]){
int i;
for(i = 1; i < 6; i++){
if(i > 3) continue ;
}
System.out.println(i);
}
}
A. 2 B. 3 C. 4 D. 5

E. 6

Answer: Option E

2. In java, ............ can only test for equality, where as .......... can evaluate any type of the
Boolean expression.

A. switch, if B. if, switch C. if, break

D. continue, if

Answer: Option A

3. What will be the output of the following program?

public class Test{


public static void main(String args[]){
int i = 0, j = 5 ;
for( ; (i < 3) && (j++ < 10) ; i++ ){
System.out.print(" " + i + " " + j );
}
System.out.print(" " + i + " " + j );
}
}

A. 06172838 B. 06172839

C. 06152535 D. Compilation Error

Answer: Option A

4. Consider the following program written in Java.

class Test{
public static void main(String args[]){
int x=7;
if(x==2); // Note the semicolon
System.out.println("NumberSeven");
System.out.println("NotSeven");
}
}

What would the output of the program be?

A. NumberSeven NotSeven B. NumberSeven

C. NotSeven D. Error

E. 7

Answer: Option A

5. Determine output:
public class Test{
public static void main(String args[]){
int i, j;
for(i=1, j=0;i<10;i++) j += i;
System.out.println(i);
}
}

A. 10 B. 11 C. 9 D. 20

E. None of these

Answer: Option A

6. What will be the output?

public class Test{


public static void main(String[] args){
int x=10, y=0;
if(x && y){
System.out.print("TRUE");
}
else{
System.out.print("FALSE");
}
}
}

A. FALSE B. TRUE C. Compilation Error

D. Runtime Error

Answer: Option C

7. What will be the value of y after execution of switch statement?

public class Test{


public static void main(String[] args){
int x = 3, y = 4;
switch(x + 3){
case 6: y = 0;
case 7: y = 1;
default: y += 1;
}
}
}

A. 1 B. 2 C. 3 D. 4 E. 0

Answer: Option B

8. What is the printout of the following switch statement?

char ch = 'a';
switch (ch){
case 'a':
case 'A': System.out.print(ch); break;
case 'b':
case 'B': System.out.print(ch); break;
case 'c':
case 'C': System.out.print(ch); break;
case 'd':
case 'D': System.out.print(ch);
}

A. abcd B. aa C. a D. ab E. abc

Answer: Option C

9. How many times will the following code print "Welcome to Examveda"?

int count = 0;
do {
System.out.println("Welcome to Examveda");
count++;
} while (count < 10);

A. 8 B. 9 C. 10 D. 11 E. 0

Answer: Option C

10. Choose the correct statement in context of the following program code.

public class Test{


public static void main(String[] args){
double sum = 0;
for(double d = 0; d < 10;){
d += 0.1;
sum += sum + d;
}
}
}

A. The program has a compile error because the adjustment is missing in the for loop.

B. The program has a compile error because the control variable in the for loop cannot be of
the double type.

C. The program runs in an infinite loop because d<10 would always be true.

D. The program compiles and runs fine.

Answer: Option D

11. Which of the following for loops will be an infinite loop?

A. for(; ;) B. for(i=0 ; i<1; i--) C. for(i=0; ; i++)

D. All of the above

Answer: Option D
To view a Sol

12.What will be the result of the following code?


public class Test{
static public void main(String args[]){ //line 2
int i, j;
for(i=0; i<3; i++){
for(j=1; j<4; j++){
i%=j;
System.out.println(j);
}
}
}
}

A. 1231 B. 1232

C. Repeatedly print 1 2 3 and cause infinite loop.

D. Compilation fails because of line 2 E. None of these

Answer: Option C

What is the value of a[1] after the following code is executed?

int[] a = {0, 2, 4, 1, 3};


for(int i = 0; i < a.length; i++)
a[i] = a[(a[i] + 3) % a.length];

A. 0 B. 1 C. 2 D. 3 E. 4

Answer: Option B

14. What will be the result of compiling and runnig the following code:

public class Test{


public static void main(String... args) throws Exception{
Integer i = 34;
int l = 34;
if(i.equals(l)){
System.out.println("true");
}else{
System.out.println("false");
}
}
}

A. true B. false C. Compiler error D. None of these

Answer: Option A

15. What all gets printed when the following program is compiled and run.

public class Test{


public static void main(String args[]){
int i, j=1;
i = (j>1)?2:1;
switch(i){
case 0: System.out.println(0); break;
case 1: System.out.println(1);
case 2: System.out.println(2); break;
case 3: System.out.println(3); break;
}
}
}

A. 0 B. 1 C. 2 D. 3 E. 12

Answer: Option E

16. What all gets printed when the following program is compiled and run?

public class Test{


public static void main(String args[]){
int i=0, j=2;
do{
i=++i;
j--;
}while(j>0);
System.out.println(i);
}
}

A. 0 B. 1 C. 2

D. The program does not compile because of statement "i=++i;" E. None of these

Answer: Option C

17. What will be the output?

public class Test{


public static void main(String args[]){
int i = 1;
do{
i--;
}while(i > 2);
System.out.println(i);
}
}

A. 1 B. 2 C. -1 D. 0

E. None of these

Answer: Option D

18. Which option, inserted at line 4, produces the output 12?

1. public class Test{


2. public static void main(String [] args){
3. int x = 0;
4. // insert code here
5. do{ } while(x++ < y);
6. System.out.println(x);
7. }
8. }

A. int y = x; B. int y = 10; C. int y = 11;

D. int y = 12; E. None of the above will allow compilation to succeed.


Answer: Option C

19. What will be the result?

1. int i = 10;
2. while(i++ <= 10){
3. i++;
4. }
5. System.out.print(i);

A. 10 B. 11 C. 12 D. 13

E. Line 5 will be never reached.

Answer: Option D
 
Prof. Swapnil S. Chaudhari
MCQ PPL

Multiple Choice Questions with Answers:-


1. Java programs are
A) Faster than others
B) Platform independent
C) Not reusable
D) Not scalable
2. Java has its origin in
A) C programming language
B) PERRL
C) COBOL
D) Oak programming language
3. Which one of the following is true for Java
A) Java is object-oriented and interpreted
B) Java is efficient and faster than C
C) Java is the choice of everyone.
D) Java is not robust.
4. The command javac is used to
A) debug a java program
B) compile a java program
C) interpret a java program
D) execute a java program
5. Java servlets are an efficient and powerful solution for creating ………….. for the web.
A) Dynamic content
B) Static content
C) Hardware
D) Both a and b
6. Filters were officially introduced in the Servlet ……………… specification.
A) 2.1
B) 2.3
C) 2.2
D) 2.4
7. Which is the root class of all AWT events
A) java.awt.ActionEvent
B) java.awt.AWTEvent
C) java.awt.event.AWTEvent
D) java.awt.event.Event
8. OOP features are
i) Increasing productivity ii) Reusability
iii) Decreasing maintenance cost iv) High vulnerability
A) 1,2 & 4
B) 1,2 & 3
C) 1, 2 & 4
D) none of the above
9. break statement is used to
i) get out of method ii) end a program
iii) get out of a loop iv) get out of the system
A) 1 & 2
B) 1,2 & 3
C) 1 & 3
D) 3
10. Native-protocol pure Java converts ……….. into the ………… used by DBMSs directly.
A) JDBC calls, network protocol
B) ODBC class, network protocol
C) ODBC class, user call
D) JDBC calls, user call
Answers:
1. B) Platform independent
2. D) Oak programming language
3. A) Java is object …… interpreted
4. B) compile a java program
5. A) Dynamic content
6. B) 2.3
7. B) java.awt.AWTEvent
8. B) 1,2 & 3
9. D) 3
10. A) JDBC calls, ……protocol

1. The JDBC-ODBC bridge allows ……….. to be used as ………..


A) JDBC drivers, ODBC drivers
B) Drivers, Application
C) ODBC drivers, JDBC drivers
D) Application, drivers
2. Which of the following is true about Java.
A) Java does not support overloading.
B) Java has replaced the destructor function of C++
C) There are no header files in Java.
D) All of the above.
3. ……………. are not machine instructions and therefore, Java interpreter generates
machine code that can be directly executed by the machine that is running the Java
program.
A) Compiled Instructions
B) Compiled code
C) byte code
D) Java mid code
4. The command javac
A) Converts a java program into binary code
B) Converts a java program into bytecode
C) Converts a java program into machine language
D) None of the above.
5. Which of the following is not the Java primitive type
A) Byte
B) Float
C) Character
D) Long double
6. Command to execute compiled java program is
A) java
B) javac
C) run
D) javaw
7. Java Servlet
i) is a key component of server-side Java development
ii) is a small pluggable extension to a server that enhances functionality
iii) runs only in Windows Operating System
iv) allows developers to customize any java enabled server
A) i, ii & iii are true
B) i, iii & iv are true
C) ii, iii & iv are true
D) i, ii & iv are true
8. Inner classes are
A) anonymous classes
B) nested classes
C) subclasses
D) derived classes
9. How many times does the following code segment execute
int x=1, y=10, z=1;
do{y–; x++; y-=2; y=z; z++} while (y>1 && z<10);
A) 1
B) 10
C) 5
D) infinite
10. State whether the following statement is true or false for EJB.
1. EJB exists in the middle-tier
2. EJB specifies an execution environment
3. EJB supports transaction processing
A) 1-true, 2. true, 3. true
B) 1- true, 2. false, 3. true
C) 1- false, 2- false, 3- false
D) 1-true, 2-true, 3-false
Answers:
1. C) ODBC drivers, JDBC drivers
2. D) All of the above.
3. C) byte code
4. B) Converts a java ….into bytecode
5. D) Long double
6. A) java
7. D) i, ii & iv are true
8. B) nested classes
9. A) 1
10. A) 1-true, 2. true, 3. True

1. All Java classes are derived from


A) java.lang.Class
B) java.util.Name
C) java.lang.Object
D) java.awt.Window
2. The jdb is used to
A) Create a jar archive
B) Debug a java program
C) Create a C header file
D) Generate java documentation
3. What would happen if “String[]args” is not included as an argument in the main
method?
A) No error
B) Compilation error
C) The program won’t run
D) Program exit
4. For the execution of DELETE SQL query in JDBC, …………. method must be used.
A) executeQuery()
B) executeDeleteQuery()
C) executeUpdate()
D) executeDelete()
5. Which method will a web browser call on a new applet?
A) main method
B) destroy method
C) execute method
D) init method
6. Which of the following is not mandatory in a variable declaration?
A) a semicolon
B) an identifier
C) an assignment
D) a data type
7. When a programming class implements an interface, it must provide behavior for
A) two methods defined in that interface
B) any methods in a class
C) only certain methods in that interface
D) all methods defined in that interface
8. In order to run JSP ……………….. is required.
A) Mail Server
B) Applet viewer
C) Java Web Server
D) Database connection
9. State true or false.
i) AWT is an extended version of swing
ii) Paint( ) of Applet class cannot be overridden
A) i-false, ii-false
B) i-false,ii-true
C) i-true, ii-false
D) i-true, ii-true
10. Prepared Statement object in JDBC used to execute……….. queries.
A) Executable
B) Simple
C) High level
D) Parameterized
Answers:
1. C) java.lang.Object
2. B) Debug a java program
3. C) Program won’t run
4. C) executeUpdate()
5. D) init method
6. C) an assignment
7. D) all methods … interface
8. C) Java Web Server
9. A) i-false, ii-false
10. D) Parameterized

1. In Java variables, if first increment of the variable takes place and then the assignment
occurs. This operation is also called…………………………
A) pre-increment
B) post-increment
C) incrementation
D) pre incrementation
2. When the operators are having the same priority, they are evaluated from ……………..
…………. in the order they appear in the expression.
A) right to left
B) left to right
C) any of the order
D) depends on the compiler

3. In Java, …………. can only test for equality, whereas ………… can evaluate any type of
Boolean expression.
A) switch, if
B) if, switch
C) if, break
D) continue, if
4. The ………………….. looks only for a match between the value of the expression and
one of its case constants.
A) if
B) match
C) switch
D) None of the above
5. System.in.read() is being used, the program must specify the ……………… clause.
A) throws.java.out.IOException
B) throws.java.in.IOException
C) throws.java.io.IOException
D) throws.java.io.InException
6. By using ………………. you can force immediate termination of a loop, bypassing the
conditional expression and any remaining code in the body of the loop.
A) Break
B) Continue
C) Terminate
D) Loop Close
7. The out object is an object encapsulated inside the …………….. class and represents the
standard output device.
A) standard
B) local
C) global
D) system
8. The third type of comment is used by a tool called ……………… for automatic
generation of documentation.
A) Java commenting
B) Java generator
C) Java doc
D) Java loc
9. In the second type, the information written in java after // is ignored by the
…………………..
A) Interpreter
B) Compiler
C) Programmer
D) All of the above
10. The compiled Java program can run on any ………………… platform having Java
Virtual Machine (JVM) installed on it.
A) program
B) java
C) hardware
D) nonjava
Answers:
1. A) pre-increment
2. B) left to right
3. A) switch, if
4. C) switch
5. C) throws.java.io.IOException
6. A) Break
7. D) system
8. C) Java doc
9. B) Compiler
10.C) hardware

1. Preparedstatement Object in JDBC is used to execute ……………………… queries.


A) executable
B) simple
C) high level
D) parameterized
2. In JDBC …………………… imports all Java classes concerned with database
connectivity.
A) javax.sql.*
B) java.mysql.*
C) java.sql.*
D) com.*
3. MS-SQL stores data in a …………………… file format.
A).DAT
B).MDB
C).MSSQL
D).OBJ
4. Ingres is a ……………………
A) Socket
B) Compiler
C) Database
D) Web server
5. In Java servlet method init( ) is called ………………… times.
A) 1
B) 2
C) 0
D) multiple
6. State true or false for Java Program.
i) All class variables are instance variables
ii) All protected methods are friendly methods
A) i-false, ii-false
B) i-false, ii-true
C) i-true, ii-false
D) i-true, ii-true
7. State true or false for Java Program.
i) Data members of an interface are by default final
ii) An abstract class has implementations of all methods defined inside it.
A) i-false, ii-false
B) i-false, ii-true
C) i-true, ii-false
D) i-true, ii-true
8. …………………… of a remotely accessible object must implement …………….
A) all methods, RemoteException
B) class, RemoteException
C) class, RemoteInterface
D) all methods, RemoteInterface
9. ………………….is the key to ……………………
A) Serialization, persistence
B) Persistence, inheritance
C) Inheritance, object
D) Persistence, serialization
10. A method named myMethod( ) that needs two integer arguments is declared as
A) public void myMethod( );
B) public void myMethod(int a, int b);
C) public void myMethod(int a, b);
D) public int myMethod(a, b);
Answers:
1. D) parameterized
2. C) java.sql.*
3. A).DAT
4. C) Database
5. A) 1
6. B) i-false, ii-true
7. C) i-true, ii-false
8. C) class, RemoteInterface
9. A) Serialization, persistence
10.B) public void myMethod(int a, int b);

1. JSP embeds in ……………. in ………………….


A) Servlet, HTML
B) HTML, Java
C) HTML, Servlet
D) Java, HTML
2. The class at the top of exception class hierarchy is ……………………..
A) ArithmeticException
B) Throwable
C) Class
D) Exception
3. In a java program, package declaration ……………….. import statements.
A) must precede
B) must succeed
C) may precede or succeed
D) none
4. The class string belongs to ………………. package.
A) java.awt
B) java.lang
C) java.applet
D) java.string
5. …………… package is used by the compiler itself. So it does not need to be imported for
use.
A) java.math
B) java.awt
C) java.applet
D) java.lang
6. State true or false for the following statements in Java.
i) Java beans slow down the software development process.
ii) Java Servlets do not have built-in multithreading feature.
A) i-false, ii-false
B) i-false, ii-true
C) i-true, ii-false
D) i-true, ii-true
7. State whether true or false.
i) init( ) of a servlet is called after a client request comes in
ii) Servlets are ultimately converted into JSP
A) i-false, ii-false
B) i-false, ii-true
C) i-true, ii-false
D) i-true, ii-true
8. What will be the result of compiling the following code?
public class MyClass{
public static void main(String args[]){
System.out.println(“In first main()”);
}
public static void main(char args[]){
System.out.println(‘a’);
}
}
A) The code will not compile and will give “Duplicate main() method declaration” error
B) The code will compile correctly but will give a runtime exception
C) The code will compile correctly and will print “In first main()” (without quotes) when it
is run
D) The code will compile correctly and will print “a” (without quotes) when it is run

9. Match the following.


a) Java 1) is a tool for debugging java program
b) Javah 2) is a tool for creating C-like header files
c) Javap 3) runs java bytecode
d) jdb 4) prints java code representation
A) a-3, b-2,c-1
B) a-3, b-2, c-4, d-1
C) a-1, b-2, c-3, d-4
D) a-2, b-1, c-3, d-4

10.State true or false.


i) init() is called after start() in applet
ii) applets are used for networking
iii) inheritance is a part of Java Foundation Classes
iv) final does not prevent inheritance
A) i-true, ii-true, iii-false, iv-true
B) i-false, ii-false, iii-false, iv-false
C) i-true, ii-true, iii-true, iv-true
D) i-true, ii-false, iii-false, iv-false

Answers:
1. D) Java, HTML
2. B) Throwable
3. A) must precede
4. B) java.lang
5. D) java.lang
6. A) i-false, ii-false
7. A) i-false, ii-false
8. C) The code will compile correctly and will print “In first main()” (without quotes) .. run
9. B) a-3, b-2, c-4, d-1
10.B) i-false, ii-false, iii-false, iv-false
1. The ……………… and ……………….. classes are abstract classes that support reading
and writing of byte streams.
A) reader, writer
B) inputstream, outputstream
C) objectinputstream, objectoutputstream
D) none
2. What is the error in the following code?
class Test
{
abstract void display( );
}

A) No error
B) Method display( ) should be declared as static
C) Test class should be declared as abstract
D) Test class should be declared as public

3. A package is a collection of
A) classes
B) interfaces
C) editing tools
D) classes and interfaces
4. Which of the following methods belong to the string class?
A) length( )
B) compare To ( )
C) equals ( )
D) All of them
5. What will be the output of the following code?
byte x=64, y;
y= (byte) (x<<2);
System.out.println(y);
A) 0
B) 1
C) 2
D) 64

6. If m and n are int type variables, what will be the result of the expression
m%n
when m=5 and n=2 ?
A) 0
B) 1
C) 2
D) None of the above
7. Which of the following control expressions are valid for an if statement?
A) An integer expression
B) A Boolean expression
C) Either A or B
D) Neither A nor B
8. The concept of multiple inheritances is implemented in Java by
A) extending two or more classes
B) extending one class and implementing one or more interfaces
C) implementing two or more interfaces
D) both B and C
9. Which of the following do not represent legal flow control statements?
A) break;
B) return;
C) exit();
D) continue outer;
10. Data input is
A) an abstract class defined in java.io
B) a class we can use to read primitive data types
C) an interface that defines methods to open files.
D) an interface that defines methods to read primitive data types.
Answers:
1. B) input stream, the output stream
2. C) Test class should be declared as abstract
3. D) classes and interfaces
4. D) All of them
5. A) 0
6. B) 1
7. B) A Boolean expression
8. D) both B and C
9. C) exit();
10.D) an interface that defines methods to read primitive data types.
1. Using which keyword we can access the value of the instance variables and class
variables of that class inside the method of that class itself.
A) super
B) final
C) this
D) either super or this
2. If a variable is declared final, it must include …………………. value.
A) integer
B) no
C) initial
D) float
3. State true or false.
i) Jpanel is a class included in awt package.
ii) Anonymous classes are mostly used for event handling.
iii) Names of anonymous classes must be unique
iv) JOptionPane is an inner class
A) i-false, ii-false, iii-true, iv-true
B) i-true, ii-false, iii-true, iv-false
C) i-false, ii-true, iii-false, iv-false
D) i-true, ii-false, iii-false, iv-true
4. In Java, a string is a ………….
A) primitive data type
B) abstract data type
C) combination of boolean
D) None of the above
5. Methods can be overloaded with a difference only in the type of the return value.
A) Not supported
B) False
C) True
D) None of the above
6. Each method in a java class must have a unique name.
A) Not necessary
B) True
C) False
D) None of the above
7. State true or false.
i) comparisons precede logical operations in java
ii) assignment operations succeed increment operations
iii) arithmetic operations succeed comparisons
iv) x precede +
A) i-true, ii-true, iii-false, iv-true
B) i-true, ii-false, iii-true, iv-false
C) i-false, ii-true, iii-false, iv-false
D) i-true, ii-false, iii-false, iv-true
8. It is an important feature of java that it always provides a default constructor to a class.
A) Not supported
B) False
C) True
D) None of the above
9. ………………….. is the key to ………………
A) Serialization, persistence
B) Persistence, inheritance
C) Inheritance, object
D) Persistence, serialization
10. State true or false.
i) The public can only be assigned to class
ii) Protected protects a statement
iii) Protected method is never accessible outside the package
iv) Friendly variable may be accessible outside the class
A) i-true, ii-true, iii-false, iv-true
B) i-true, ii-false, iii-true, iv-false
C) i-false, ii-true, iii-false, iv-false
D) i-true, ii-false, iii-false, iv-true
Answers:
1. C) this
2. C) initial
3. C) i-false, ii-true, iii-false, iv-false
4. B) abstract data type
5. B) False
6. A) Not necessary
7. A) i-true, ii-true, iii-false, iv-true
8. C) True
9. A) Serialization, persistence
10.C) i-false, ii-true, iii-false, iv-false
1. In java a ………………….. is a sequence of characters.
Java Objective Questions with Answers
Schema of the general architecture of a program running in a Java Virtual Machine
(Photo credit: Wikipedia)
A) string
B) arrayChar
C) groupChar
D) collection
2. Java programs perform I/O through ………..
A) I/O methods
B) I/O package
C) streams
D) compiler
3. What is the byte code in the context of Java?
A) The type of code generated by a Java compiler
B) The type of code generated by a Java Virtual Machine
C) It is another name for Java source file
D) It is the code written within the instance methods of a class
4. Which of the following statements about abstract methods/classes in Java is true?
A) An abstract class cannot be instantiated.
B) Constructors can be abstract.
C) A subclass of an abstract class must define the abstract methods.
D) Static methods may be declared abstract.
5. Which of the following statement is false?
A) The sleep() method should be enclosed in try ……… catch block
B) The yield() method should be enclosed in try ……… catch block.
C) A thread can be temporarily suspended from running by using the wait() method.
D) A suspended thread using suspend() method can be revived using the resume()
method.
6. The new operator dynamically allocates …………..for an object and returns a
reference to it.
A) classes
B) variables
C) memory
D) none of the above
7. Which of the following statements correctly describes an interface?
A) It’s a concrete class
B) It’s a superclass
C) It’s a type of abstract class
D) It’s a subclass
8. What is the priority of the Garbage collector thread of JDK?
A) Low Priority
B) Highest Priority
C) Medium Priority
D) Decided at run time
9. ……………. is a feature that allows one interface to be used for a general class of
actions.
A) Class
B) Inheritance
C) Polymorphism
D) Interface
10. The default package that is implicitly called in a java program is ………….
A) java. Lang
B) java.System
C) java. Window
D) java.Lang.System
Answers:
1. In java a ………………….. is a sequence of characters.
B) arrayChar
2. Java programs perform I/O through ………..
C) streams
3. What is the byte code in the context of Java?
A) The type of code generated by a Java compiler
4. Which of the following statements about abstract methods/classes in Java is true?
A) An abstract class cannot be instantiated.
5. Which of the following statement is false?
B) The yield() method should be enclosed in try ……… catch block.
6. The new operator dynamically allocates …………..for an object and returns a
reference to it.
C) memory
7. Which of the following statements correctly describes an interface?
C) It’s a type of abstract class
8. What is the priority of the Garbage collector thread of JDK?
A) Low Priority
9. ……………. is a feature that allows one interface to be used for a general class of
actions.
C) Polymorphism
10. The default package that is implicitly called in a java program is ………….
A) java. Lang

1. Java was developed by the company


A) Sun Microsystems
An image representing Sun Microsystems as depict…
Sun Microsystems Image CrunchBase
B) Microsoft
C) Micro-tech
D) IBM
2. What is the file extension of a compiled java program?
A) .class
B) .java
C) .css
D) .html
3. ……………… keyword is used to invoke the current object.
A) New
B) That
C) This
D) Me
4. Which of the function is used to convert string to Number in java program?
A) to Number()
B) conString()
C) valueOf()
D) toString()
5. What are the part in executing a Java program and their purposes?
A) Java Compiler
B) Java Interpreter
C) Java Pre-processor
D) Directive Pre-processor
6. Method overloading is one of the ways that Java supports …………
A) encapsulation
B) class
C) inheritance
D) polymorphism
7. Java support RMI. What does this RMI stand for?
A) Random Memory Interface
B) Remote Method Invocation
C) Random Method Invocation
D) Remote Memory Interface
8. Which of the following represent legal flow control statements?
A) break;
B) break();
C) continue(inner);
D) exit();
9. The keywords reserved but used in the initial version of Java are
A) union
B) const
C) inner
D) goto
10. What is the default return type of main().
A) void
B) double
C) float
D) int
Answers:
1. Java was developed by the company
A) Sun Microsystems
2. What is the file extension of a compiled java program?
A) .class
A 3. ……………… keyword is used to invoke the current object.
C) This
4. Which of the function is used to convert string to Number in java program?
C) valueOf()
5. What are the part of executing a Java program and their purposes?
A) Java Compiler
6. Method overloading is one of the ways that Java supports …………
D) polymorphism
7. Java support RMI. What does this RMI stand for?
B) Remote Method Invocation
8. Which of the following represent legal flow control statements?
A) break;
9. The keywords reserved but used in the initial version of Java are
A) union
10. What is the default return type of main().
D) int
1. If m and n are int type variables, what will be the result of the expression m%n when
m=-14 and n=-3?
A) 4
B) 2
C) -2
D) -4
2. Consider the following code
if(number>=0)
if(number>0)
system.out.println(“Number is positive”);
else
system.out.println(“Number is negative”);
What will be the output if number is equal to 0?
A) Number is negative
B) Number is positive
C) Both A and B
D) None of the above
3. Consider the following code:
char c=’a’;
switch (c)
{
case ‘a’;
system.out.println(“A”);
case ‘b’;
system.out.println(“B”);
default;
system.out.println(“C”);
}
For this code, which of the following statement is true?
A) The output will be A
B) The output will be A followed by B
C) The output will be A, followed by B, and then followed by C
D) Code is illegal and therefore will not compile
4. Consider the following class definition.
class Student extends String
{
}
What happens when we try to compile this class?
A) Will not compile because the class body is not defined
B) Will not compile because the class is not declared public.
C) Will not compile because the string is abstract.
D) Will not compile because the string is final.
5. What is wrong in the following class definitions?
abstract class print
{
abstract show();
}
class Display extends Print
{
}
A) Nothing is wrong
B) Wrong. Method show() should have a return type
C) Wrong. Method show() is not implemented in Display
D) Wrong. The display does not contain any numbers.
6. What is the error in the following class definitions?
abstract class XY
{
abstract sum(int x, int y){ }
}
A) The class header is not defined properly
B) The constructor is not defined
C) A method is not defined properly
D) No error.
7. Which of the following statements are true?
i) We cannot use abstract classes to instantiate objects directly.
ii) The abstract methods of an abstract class must be defined in its subclass.
iii) We cannot declare abstract constructors.
iv) We may declare abstract static methods.
A) Line i only
B) Line ii only
C) Line i and ii only
D) Line i, ii and iii only
8. We would like to make a member of a class visible in all subclasses regardless of
what package they are in. Which one of the following keywords would archive this?
A) Private
B) Protected
C) Public
D) Private Protected
9. The use of a protected keyword to a member in a class will restrict its visibility as
follows:
A) Visible only in the class and its subclass in the same package.
B) Visible only inside the same package.
C) Visible in all classes in the same package and subclasses in other packages
D) Visible only in the class where it is declared.
10. Consider the following code:
interface Area
{
float compute (float x, float y);
}
class Room implements Area
{
float compute (float x, float y)
{
return(x&y);
}
}
What is wrong with the code?
A) The interface definition is incomplete
B) Method compute() in interface Area should be declared public
C) Method compute() in class Room should be declared public
D) All the above
Answers:
1. If m and n are int type variables, what will be the result of the expression m%n when
m=-14 and n=-3?
C) -2
2. Consider the following code
if(number>=0)
if(number>0)
system.out.println(“Number is positive”);
else
system.out.println(“Number is negative”);
What will be the output if number is equal to 0?
A) Number is negative
3. Consider the following code:
char c=’a’;
switch (c)
{
case ‘a’;
system.out.println(“A”);
case ‘b’;
system.out.println(“B”);
default;
system.out.println(“C”);
}
For this code, which of the following statement is true?
B) The output will be A followed by B
4. Consider the following class definition.
class Student extends String
{
}
What happens when we try to compile this class?
D) Will not compile because the string is final.
5. What is wrong in the following class definitions?
abstract class print
{
abstract show();
}
class Display extends Print
{
}
C) Wrong. Method show() is not implemented in Display
6. What is the error in the following class definitions?
abstract class XY
{
abstract sum(int x, int y){ }
}
C) A method is not defined properly
7. Which of the following statements are true?
i) We cannot use abstract classes to instantiate objects directly.
ii) The abstract methods of an abstract class must be defined in its subclass.
iii) We cannot declare abstract constructors.
iv) We may declare abstract static methods.
D) Line i, ii and iii only
8. We would like to make a member of a class visible in all subclasses regardless of
what package they are in. Which one of the following keywords would archive this?
D) Private Protected
9. The use of a protected keyword to a member in a class will restrict its visibility as
follows:
C) Visible in all classes in the same package and subclasses in other packages
10. Consider the following code:
interface Area
{
float compute (float x, float y);
}
class Room implements Area
{
float compute (float x, float y)
{
return(x&y);
}
}
What is wrong with the code?
C) Method compute() in class Room should be declared public
1. A java program is first ……………. and ……………….
A) executed, run
B) compiled, run
C) run, compiled
D) interpreted, compiled
2. Byte code is also a ………..
A) machine code
B) bit code
C) cryptographic code
D) none
3. A private class is accessible from inside a ……………..
A) package
B) class
C) method
D) none
4. Consider the statement “x=(a>b)?a:b”, then the value of x is 19 if a=19 and b=12
A) true
B) not supported
C) false
D) none of the above
5. Adapter classes are used for ……………
A) code redundancy
B) code reduction
C) code organization
D) none
6. ………………… inheritance is enabled by an interface in java.
A) min level
B) multiple
C) low level
D) none
7. ……………….. is generated if a button is clicked in AWT.
A) ItemEvent
B) WindowEvent
C) ActionEvent
D) MouseEvent
8. Using which keyword we can access the value of the instance and class variables of
that class inside the method of that class itself.
A) super
B) final
C) this
D) either super or this
9. If a variable is declared FINAL, it must include ……………………. value.
A) integer
B) number
C) initial
D) float
10. In Java, a string is a
A) primitive data type
B) abstract data type
C) combination of boolean
D) None of the above
11. Methods can be overloaded with a difference only in the type of the return value
A) Not supported
B) False
C) True
D) None of the above
12. Each method in a java class must have a unique name
A) Not necessarily
B) True
C) False
D) None of the above
13. It is an important feature of java that it always provides a default constructor to a
class
A) Not supported
B) False
C) True
D) None of the above
14. If one or more abstract methods are not implemented in an abstract class, then the
subclass is also abstract.
A) Not necessarily
B) False
C) True
D) None of the above
15. The life cycle of an applet is described by ……………… methods.
A) six
B) three
C) four
D) five
16. Which of the following is not a subclass of “writer” stream.
A) FileWriter
B) LineWriter
C) BufferedWriter
D) PrintWriter
17. In Java RMI, a stub is located in
A) client machine
B) proxy
C) server
D) none of the above
18. The new keyword will
i) create an instance of an object
ii) create an instance of a class
iii) assign memory to an array
iv) call the destructor of a class
A) I and iii
B) ii and iii
C) I and ii
D) ii, iii and iv
19. ODBC stands for
A) Object Data Binding Command
B) Open Database Console
C) Open Database Connectivity
D) Open Database Command
20. Continue statement can be used
i) anywhere inside the main method
ii) anywhere inside the class
iii) within instance methods
iv) only within looping statements
A) I, ii and iii
B) I and iv
C) only iv
D) ii, iii and iv
1) Which of the following statement is/are true?
i) The name of a java program file must match the name of the class with the extension
.java
ii) Two methods cannot have the same name in java
A. i- only
B. ii- only
C. Both I and ii
D. None of the above
2) Which of the following represent(s) of a hexadecimal number?
A. 570
B. (hex)5
C. 0X9F
D. 0X5
3) …….. can appear only wherein the body of a Java method.
A. definition
B. declaration
C. determine
D. package
4) State whether the following statement is True or False?
i) The modulus operator(%) can be used only with integer operands
ii) Declarations can appear anywhere in the body of a Java method
A. i-True, ii-False
B. i-False, ii-True
C. i-True, ii-True
D. i-False, ii-False
5) Which of the following will produce a value of 22 if x=22.9?
A. ceil(x)
B. round(x)
C. rint(x)
D. abs(x)
6) …….. is passed to a method by use of call by reference.
A. Variables
B. Objects
C. Methods
D. Operators
7) Using the keyboard interface you can fully abstract …
A. Method
B. Keyword
C. Class
D. Variables
8) State True or False for the following statements in java.
i) All the bitwise operators have the same level of precedence in java
ii) The default can is always required in the switch selection structure.
A. i-True, ii-False
B. i-False, ii-True
C. i-True, ii-True
D. i-False, ii-False
9) Java is designed for ………. environment of the Internet.
A. Development
B. Deduction
C. Distributed
D. Web Design
10) Which of the following will produce a value of 10 if x=9.7?
A. floor(x)
B. abs(x)
C. rint(x)
D. round(x)
11) Variable declared as ……… do not occupy on a per-instance basis.
A. Static
B. Final
C. Abstract
D. Code
12) Which of the following is not keyword?
A. NULL
B. implements
C. protected
D. switch
13) The …… statement is used to terminate a statement sequence.
A. Break
B. Switch
C. Continue
D. Wait
14) Multidimensional arrays are actually ……
A. Arrays of element
B. Arrays of variable
C. Arrays of arrays
D. Arrays of variable
15) Which of the following keywords are used to control access to a class member?
i) default ii) abstract iii) protected iv) interface v) public
A. I, ii and iii only
B. ii, iii and iv only
C. iii, iv and v only
D. ii, iii and v only
16) ……. is used for initializing the value to the string object.
A. Character Literals
B. String Literals
C. String group literals
D. Group literals
17) Which of the following methods can be used to remove a component from the
display?
A. delete()
B. remove()
C. disappear()
D. hide()
18) Which of the following statements are valid array declaration?
i) int number(); ii) float average[ ];
iii) double[]marks; iv) counter int[];
A. I and ii
B. ii and iii
C. iii and iv
D. All I, ii, iii and iv
19) Which of the following methods belong to the String class?
A. length()
B. CompareTo()
C. equals()
D. substring()
E. All of the above
20) Which of the following is NOT represent legal flow control statements?
A. break;
B. break();
C. continue outer;
D. return;
Answers:
1) A. i- only
2) A. 570
3) B. declaration
4) B. i-False, ii-True
5) C. int(x)
6) B. Objects
7) C. Class
8) D. i-False, ii-False
9) C. Distributed
10) D. round(x)
11) B. Final
12) A. NULL
13) A. Break
14) C. Arrays of arrays
15) D. ii, iii and v only
16) B. String literals
17) D. hide()
18) B. ii and iii
19) E. All of the above
20) B. break();
1) An object is an …….. of class.
A. instance
B. implement
C. inheritance
D. invoke
2) ……. is the wrapper class.
A. Random
B. Byte
C. Vector
D. String
3) Class is a …….. entity.
A. logical
B. Physical
C. up normal
D. collection of
4) Which of the following denotes a Javadoc comment?
A. //#
B. /*
C. /**
D. //**
5) Object is a …….. entity.
A. normal
B. physical
C. logical
D. normal
6) One interface can inherit another by use of the keyword ……..
A. public
B. extends
C. method name
D. class name
7) ……… must be the first non-comment statement in the file
A. package
B. class
C. object
D. declaration
8) In Java thread to thread, communication is called ….
A. passing
B. sending
C. messaging
D. calling
9) Every method of a ……….. is implicitly final.
A. static class
B. dynamic class
C. final class
D. abstract class
10) The string is defined in ……… namespace.
A. java.Lang
B. java.String
C. java.Char
D. java.Awt
11) A ……… an object cannot be modified after it is created.
A. double
B. int
C. string
D. main
12) ……… is a special member function.
A. method
B. class
C. use defined function
D. constructor
13) A …….. class may not have any abstract method.
A. abstract
B. static
C. final
D. public
14) Keyword ………. is always a reference to the object.
A. new
B. this
C. invoke
D. class
15) ……… operators are overloaded for string objects?
A. -,+
B. +,=
C. <<,>>
D. ++,–
16) ……. is a small unit of a process.
A. method
B. thread
C. applet
D. steam
17) ……. is valid for if statement?
A. An integer expression
B. A Boolean expression
C. A character expression
D. A legal expression
18) A wrapper class is a wrapper around a ……. data type.
A. normal
B. central
C. primitive
D. concrete
19) …….. statement is valid for array declaration.
A. int number();
B. float number();
C. float number[];
D. count int[];
20) ……. operators which concatenates two strings.
A. +
B. ++
C. –
D. +-
Answers:
1) A. instance
2) B. Byte
3) A. logical
4) C. /**
5) B. physical
6) B. extends
7) A. package
8) C. messaging
9) C. final class
10) A. java.Lang
11) C. string
12) D. constructor
13) C. final
14) B. this
15) B. +,=
16) B. thread
17) B. A Boolean expression
18) C. primitive
19) C. float number[];
20) A. +
 
1. Which of these selection statements test only for equality? 

a) if 

b) switch 

c) if & switch 

d) none of the mentioned 

View Answer 

Answer: b 

Explanation: Switch statements checks for equality between the controlling variable and its constant 
cases. 

2. Which of these are selection statements in Java? 

a) if() 

b) for() 

c) continue 

d) break 

View Answer 

Answer:a 

Explanation: Continue and break are jump statements, and for is an looping statement. 

3. Which of the following loops will execute the body of loop even when condition controlling the loop is 
initially false? 

a) do‐while 

b) while 

c) for 

d) none of the mentioned 

View Answer 
 

Answer: a 

Explanation: None. 

4. Which of these jump statements can skip processing remainder of code in its body for a particular 
iteration? 

a) break 

b) return 

c) exit 

d) continue 

View Answer 

Answer: d 

Explanation: None. 

5. Which of these statement is incorrect? 

a) switch statement is more efficient than a set of nested ifs 

b) two case constants in the same switch can have identical values 

c) switch statement can only test for equality, whereas if statement can evaluate any type of boolean 
expression 

d) it is possible to create a nested switch statements 

View Answer 

Answer: b 

Explanation: No two case constants in the same switch can have identical values. 

6. What is the output of this program? 

    class selection_statements  
    { 

        public static void main(String args[]) 

        { 

            int var1 = 5;  

            int var2 = 6; 

            if ((var2 = 1) == var1) 

                System.out.print(var2); 

            else  

                System.out.print(++var2); 

        }  

    } 

a) 1 

b) 2 

c) 3 

d) 4 

View Answer 

Answer:b 

Explanation: var2 is initialised to 1. The conditional statement returns false and the else part gets 
executed. 

output: 

$ javac selection_statements.java 

$ java selection_statements 

7. What is the output of this program? 

 
    class comma_operator  

    { 

        public static void main(String args[])  

        {     

             int sum = 0; 

             for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1) 

                 sum += i; 

        System.out.println(sum); 

        }  

    } 

a) 5 

b) 6 

c) 14 

d) compilation error 

View Answer 

Answer: b 

Explanation: Using comma operator , we can include more than one statement in the initialization and 
iteration portion of the for loop. Therefore both ++i and j = i + 1 is executed i gets the value – 0,1,2,3,4 & 
j gets the values ‐0,1,2,3,4,5. 

output: 

$ javac comma_operator.java 

$ java comma_operator 

68. What is the output of this program? 

    class jump_statments  
    { 

        public static void main(String args[])  

        {         

             int x = 2; 

             int y = 0; 

             for ( ; y < 10; ++y)  

             { 

                 if (y % x == 0)  

                     continue;   

                 else if (y == 8) 

                      break; 

                 else 

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

             } 

        }  

    } 

a) 1 3 5 7 

b) 2 4 6 8 

c) 1 3 5 7 9 

d) 1 2 3 4 5 6 7 8 9 

View Answer 

Answer:c 

Explanation: Whenever y is divisible by x remainder body of loop is skipped by continue statement, 
therefore if condition y == 8 is never true as when y is 8, remainder body of loop is skipped by continue 
statements of first if. Control comes to print statement only in cases when y is odd. 
output: 

$ javac jump_statments.java 

$ java jump_statments 

1 3 5 7 9 

9. What is the output of this program? 

class Output  

        public static void main(String args[])  

        {     

           final int a=10,b=20; 

          while(a<b) 

          { 

  

          System.out.println("Hello"); 

          } 

          System.out.println("World"); 

  

        }  

a) Hello 

b) run time error 

c) Hello world 

d) compile time error 

View Answer 
 

Answer: d 

Explanation: Every final variable is compile time constant. 

10. What is the output of this program? 

    class Output  

    { 

        public static void main(String args[])  

        {     

             int a = 5; 

             int b = 10; 

             first:  

             { 

                second:  

                { 

                   third:  

                   {  

                       if (a ==  b >> 1) 

                           break second; 

                   } 

                   System.out.println(a); 

                } 

                System.out.println(b); 

             } 

        }  
    } 

a) 5 10 

b) 10 5 

c) 5 

d) 10 

Answer: d 

Explanation: b >> 1 in if returns 5 which is equal to a i:e 5, therefore body of if is executed and block 
second is exited. Control goes to end of the block second executing the last print statement, printing 10. 

output: 

$ javac Output.java 

$ java Output 

10 

1. What would be the output of the following codesnippet if variable a=10? 

if(a<=0) 

   if(a==0) 

   { 

     System.out.println("1 "); 

   } 

   else  

   {  

      System.out.println("2 "); 

   } 

System.out.println("3 "); 
a) 1 2 

b) 2 3 

c) 1 3 

d) 3 

View Answer 

Answer: d 

Explanation: Since the first if condition is not met, control would not go inside if statement and hence 
only statement after the entire if block will be executed. 

2. The while loop repeats a set of code while the condition is not met? 

a) True 

b) False 

View Answer 

Answer: b 

Explanation: While loop repeats a set of code only until condition is met. 

3. What is true about break? 

a) Break stops the execution of entire program 

b) Break halts the execution and forces the control out of the loop 

c) Break forces the control out of the loop and starts the execution of next iteration. 

d) Break halts the execution of the loop for certain time frame 

View Answer 

Answer: b 

Explanation: Break halts the execution and forces the control out of the loop. 

4. What is true about do statement? 
a) do statement executes the code of a loop at least once 

b) do statement does not get execute if condition is not matched in the first iteration 

c) do statement checks the condition at the beginning of the loop 

d) do statement executes the code more than once always 

View Answer 

Answer: a 

Explanation: Do statement checks the condition at the end of the loop. Hence, code gets executed at 
least once. 

5. Which of the following is used with switch statement? 

a) Continue 

b) Exit 

c) break 

d) do 

View Answer 

Answer: c 

Explanation: Break is used with switch statement to shift control out of switch. 

6. What is the valid data type for variable “a” to print “Hello World”? 

switch(a) 

   System.out.println("Hello World"); 

a) int and float 

b) byte and short 
c) char and long 

d) byte and char 

View Answer 

Answer: d 

Explanation: The switch condition would only meet if variable “a” is of type byte or char. 

7. Which of the following is not a decision making statement? 

a) if 

b) if‐else 

c) switch 

d) do‐while 

View Answer 

Answer: d 

Explanation: do‐while is an iteration statement. Others are decision making statements. 

8. Which of the following is not a valid jump statement? 

a) break 

b) goto 

c) continue 

d) return 

View Answer 

Answer: b 

Explanation: break, continue and return transfer control to another part of the program and returns 
back to caller after execution. However, goto is marked as not used in Java. 

9. From where break statement causes an exit? 
a) Only from innermost loop 

b) Terminates a program 

c) Only from innermost switch 

d) From innermost loops or switches 

View Answer 

Answer: d 

Explanation: The break statement causes an exit from innermost loop or switch. 

10. Which of the following is not a valid flow control statement? 

a) exit() 

b) break 

c) continue 

d) return 

View Answer 

Answer: a 

Explanation: exit() is not a flow control statement in Java. exit() terminates the currently running JVM. 

Which of these class is superclass of String and StringBuffer class? 

A. java.util B. java.lang C. ArrayList D. None of the mentioned 

Answer & Explanation 

Answer: Option B 

Explanation: 

java.lang 

2. Which of these operators can be used to concatenate two or more String objects? 

A. + B. += C. & D. || 
Answer & Explanation 

Answer: Option A 

Explanation: 

operator + is used to concatenate strings, Example String s = “i ” + “like ” + “java”; String s contains “I like 
java”. 

3. Which of these method of class String is used to obtain length of String object? 

A. get() B. Sizeof() C. lengthof() D. length() 

Answer & Explanation 

Answer: Option D 

Explanation: 

Method length() of string class is used to get the length of the object which invoked method length(). 

4. Which of these method of class String is used to extract a single character from a String object? 

A. CHARAT() B. charat() C. charAt() D. ChatAt() 

Answer & Explanation 

Answer: Option C 

Explanation: 

charAt() 

5. Which of these constructors is used to create an empty String object? 

A. String() B. String(void) C. String(0) D. None of the mentioned 

Answer & Explanation 

Answer: Option A 

Explanation: 

String() 

7. Which of these method of class String is used to compare two String objects for their equality? 

A. equals() B. Equals() C. isequal() D. Isequal() 

Answer & Explanation 
Answer: Option A 

Explanation: 

equals() 

9. Which of these method of class String is used to check weather a given object starts with a particular 
string literal? 

A. startsWith() B. endsWith() C. Starts() D. ends() 

Answer & Explanation 

Answer: Option A 

Explanation: 

Method startsWith() of string class is used to check whether the String in question starts with a specified 
string. It is specialized form of method regionMatches() 

10. What is the value returned by unction compareTo() if the invoking string is less than the string 
compared? 

A. zero B. value less than zero C. value greater than zero D. None of the mentioned 

Answer & Explanation 

Answer: Option B 

Explanation: 

compareTo() function returns zero when both the strings are equal, it returns a value less than zero if 
the invoking string is less than the other string being compared and value greater than zero when 
invoking string is greater than the string compared to. 

13. 

What will s2 contain after following lines of code? 

 String s1 = “one”; 

 String s2 = s1.concat(“two”) 

 
A. one B. two C. onetwo D. twoone 

Answer & Explanation 

Answer: Option C 

Explanation: 

Two strings can be concatenated by using concat() method. 

14. Which of these method of class String is used to remove leading and trailing whitespaces? 

A. startsWith() B. trim() C. Trim() D. doTrim() 

Answer & Explanation 

Answer: Option B 

Explanation: 

trim() 

15. What is the value returned by function compareTo() if the invoking string is less than the string 
compared? 

A. zero B. value less than zero C. value greater than zero D. None of the mentioned 

Answer & Explanation 

Answer: Option B 

Explanation: 

compareTo() function returns zero when both the strings are equal, it returns a value less than zero if 
the invoking string is less than the other string being compared and value greater than zero when 
invoking string is greater than the string compared to. 

You might also like