0% found this document useful (0 votes)
464 views23 pages

Java MCQ by

This document contains 30 multiple choice questions related to Java programming. The questions cover topics like data types, operators, control structures, OOP concepts, strings, exceptions and date/time handling in Java. Sample questions include determining the output of code snippets, identifying valid Java statements, and recognizing object-oriented programming principles.

Uploaded by

hitesh
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
464 views23 pages

Java MCQ by

This document contains 30 multiple choice questions related to Java programming. The questions cover topics like data types, operators, control structures, OOP concepts, strings, exceptions and date/time handling in Java. Sample questions include determining the output of code snippets, identifying valid Java statements, and recognizing object-oriented programming principles.

Uploaded by

hitesh
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 23

LNCT GROUP OF COLLEGES JAVA MCQ QUESTION

Ques 1) :-

Which of these literals can be contained in float data type variable?


A. -1.7e+308
B. -3.4e+038
C. +1.7e+308
D. -3.4e+050

Ques 2) :-
What will be the output of these statement?
class output {
publicstaticvoidmain(String args[])
{
double a, b,c;
a = 3.0/0;
b = 0/4.0;
c=0/0.0;

System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}

A. Infinity
B. 0.0
C. NaN
D. all of the mentioned

Ques 3) :-
What is the output of this program?
class area {
publicstaticvoidmain(String args[])
{
double r, pi, a;
r = 9.8;
pi = 3.14;
a = pi * r * r;
System.out.println(a);
}
}
LNCT GROUP OF COLLEGES JAVA MCQ QUESTION

A. 301.5656
B. 0301
C. 301.56
D. 301.56560000

Ques 4) :-
What is the output of this program?
class Output {
public static void main(String args[])
{
int x=y=z=20;

}
}
A. compile and runs fine

B. 20
C. run time error
D. compile time error

Ques 5) :-
What will be the output of following program?
public class temp
{
public static void main(String agrs[])
{
for(inti=1; i<=10; i++);
System.out.print(i);
}
}

A. 12345678910
B.11
C. Error
D.1 2 3 4 5 6 7 8 9 10
LNCT GROUP OF COLLEGES JAVA MCQ QUESTION

Ques 6) :-
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. 1

B4

C.6

D. Error

Ques 7) :-

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

Ques 8) :-.
What is the output of this program?
classselection_statements{
publicstaticvoidmain(Stringargs[])
{
int var1 =5;
int var2 =6;
if((var2 =1)== var1)
System.out.print(var2);
else
System.out.print(var2++ + ++var2);
}
LNCT GROUP OF COLLEGES JAVA MCQ QUESTION
A. 1
B. 2
C. 3
D. 4

Ques 9) :-.
What is the output of this program?
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

Ques 10) :-.

What is the output of these following lines of codes.

public static void main(String args[])


{
int a = 5;
int b = 10;
a=a++ + a++ + ++a + ++a;
b=a++ + ++b;
System.out.println(a+" "+b);

}
LNCT GROUP OF COLLEGES JAVA MCQ QUESTION

A. 27 38

B. 28 38

C. 29 39

D. 28 37

Ques 11):-
Given:
class TestA {
public void start() { System.out.println("TestA"); }
}
public class TestB extends TestA {
public void start()
{ System.out.println("TestB"); } public static
void main(String[] args) { ((TestA) new
TestB()).start(); }

}
What is the result?
A. TestA
B. TestB
C. Compilation fails.
D. An exception is thrown at runtime.

Ques 12):-
Which of the following concept of oops allows compiler to insert arguments in a
function call if it is not specified?

A. Call by value

B. Call by reference

C. Default arguments

D. Call by pointer

Ques 13):-

Which of these is not a correct statement?


A. Every class containing abstract method must be declared abstract.
B. Abstract class defines only the structure of the class not its implementation.
C. Abstract class can be initiated by new operator.
D. Abstract class can be inherited.
LNCT GROUP OF COLLEGES JAVA MCQ QUESTION

Ques 14):-
Suppose the class Undergraduate extends the class Student which extends the class
Person.
Given the following variable declaration:
Person p = new Person();
Student s = new Student();
Undergraduate ug = new Undergraduate();
Which of the following assignments are legal?
I. p = ug;
II. p = new Undergraduate();
III. ug = new Student();
IV. ug = p;
V. s = new Person();

A . III and IV
B. I and IV
C. I and II
D. II, III and V

Ques 15):-
Given:
public static void parse(String str) {
try {
float f = Float.parseFloat(str);
} catch (NumberFormatException
nfe) { f = 0;
} finally {
System.out.println(f);
}
}
public static void main(String[] args) {
parse("invalid");
}
What is the result?
A. 0.0
B. Compilation fails.
C. A ParseException is thrown by the parse method at runtime.
D. A NumberFormatException is thrown by the parse method at runtime.

Ques 16):-

What is the output of this program?


class area {
public static void main(String args[])
{
double r, pi, a;
r = 9.8;
pi = 3.14;
a = pi * r * r;
System.out.println(a);
}
}
A)301.5656
B)301
C)301.56
D) 301.56560000

Ques 17) :-
In Java, the actual method executed is determined by the type of the object and not
the type of the reference.
A. True
B. False

Ques 18) :-
Which of these method of class StringBuffer is used to get the length of sequence of
characters?
a) length()
b) capacity()
c) Length()
d) Capacity()

Ques 19) :- What is the output of this program?

class Output {

public static void main(String args[]) {

char a[] = {'a', '5', 'A', ' '};

System.out.print(Character.isDigit(a[0]) + " ");

System.out.print(Character.isWhitespace(a[3]) + " ");

System.out.print(Character.isUpperCase(a[2])); } }

A. true false true

B. false true true


LNCT GROUP OF COLLEGES JAVA MCQ QUESTION

C. true true false

D. false false false

Ques 20) :-
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. None of these

Ques 21) :-
toString() method is defined in -
A. java.lang.String
B. java.lang.object
C. java.lang.util
D. None of these

Ques 22) :- What is the output of this program?

class Output {

public static void main(String args[]) {

Double i = new Double(257.578123456789);

float x = i.floatValue();

System.out.print(x); } }

a) 0

b) 257.0
c) 257.57812

d) 257.578123456789
LNCT GROUP OF COLLEGES JAVA MCQ QUESTION

Ques 23) :-
What will be the output?
String str1 = "abcde";
System.out.println(str1.substring(1, 3)); 

A. abc
B. bc
C. cbd
D. None of these

Ques 24) :- What is the output of this program?

class Output {

public static void main(String args[]) {

int a = Character.MAX_VALUE;

System.out.print((char)a); }}

a) <

b) >

c) ?

d) $

Ques 25) :- What is the output of this program?

class Output {

public static void main(String args[]) {

String str = "true";

boolean x = Boolean.valueOf(str);

System.out.print(x); } }

A. true

B. flase

C. Compilation Error

D. Runtime Error
LNCT GROUP OF COLLEGES JAVA MCQ QUESTION

Ques 26) :-
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

Ques 27) :- What is the output of this program?

class Output {

public static void main(String args[]) {

Double y = new Double(257.57812);

Double i = new Double(257.578123456789);

try {

int x = i.compareTo(y);

System.out.print(x);

catch(ClassCastException e) {

System.out.print("Exception");

}}}

a) 0

b) 1

c) Exception

d) None of the mentioned


LNCT GROUP OF COLLEGES JAVA MCQ QUESTION

Ques 28) :-
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

Ques 29) :-
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, false
C. true,false
D. false,true
LNCT GROUP OF COLLEGES JAVA MCQ QUESTION

Ques 30) :- Given that Calendar.MONTH starts with January == 0, and given:

import java.util.*;
public class Wise {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
c.set(1999,11,25);
c.roll(Calendar.MONTH, 3);
c.add(Calendar.DATE, 10);
System.out.println(c.getTime());
}}
And, if the program compiles, what date is represented in the output?
A) March 4, 1999
B) April 4, 1999
C) March 4, 2000
D) April 4, 2000
LNCT GROUP OF COLLEGES JAVA MCQ QUESTION

Ques 31) :- Given:

String s = "";
if(011 == 9) s += 4;
if(0x11 == 17) s += 5;
Integer I = 12345;
if(I.intValue() == Integer.valueOf("12345")) s
+= 6; System.out.println(s);
What is the result?
A) 5
B) 46
C) 56
D) 456
E) Compilation fails.

Ques 32) :- Given:

public class Choosy {


public static void main(String[] args) {
String result = "";
int x = 7, y = 8;
if(x == 3) { result += "1"; }
else if (x > 9) { result += "2"; }
else if (y < 9) { result += "3"; }
else if (x == 7) { result += "4"; }
else { result += "5"; }
System.out.println(result);
}}
What is the result? (Choose all that apply.)
A) 3
B) 34
LNCT GROUP OF COLLEGES JAVA MCQ QUESTION

C) 35
D) 345

Ques 33) :- Which of the following concept of oops allows compiler to insert arguments in a
function call if it is not specified?
A.) Call by value
B.) Call by reference
C.) Default arguments
D.)
Call by pointer

Ques 34) :- Given:

import java.io.*;
import java.util.*;
import static java.lang.Short.*;
import static java.lang.Long.*;
public class MathBoy {
public static void main(String[] args) {
long x = 123456789;
short y = 22766; // maximum value of a short is 32767
System.out.printf("%1$+10d %2$010d ", x,
MAX_VALUE - y); System.out.println(new Date()); } }

Which are true? (Choose all that apply.)


A) Compilation fails.
B) The output will include "+"
C) The output will include "10001"
D) The output will include "0000010001"
LNCT GROUP OF COLLEGES JAVA MCQ QUESTION

Ques 35) :- Given:

interface TestA { String toString(); }


public class Test {
public static void main(String[] args) {
System.out.println(new TestA() {
public String toString() { return "test"; }
});
}}
What is the result?
A) test
B) null
C) An exception is thrown at runtime.
D) Compilation fails because of an error in line 1.

Ques 36) :-

Which of the following best defines the term inheritance?


A) when one class manipulates or changes objects of another class
B) when a member variable of class A makes use of an object of type class B
C) the collection of a parent class along with its descendants
D)the process by which the properties, methods, and events of one class are passed onto
another class.

Ques 37) :-

Which of the following best defines the term hierarchy?


A) the process by which the properties, methods, and events of one class are passed onto
another class
B) when one class manipulates or changes objects of another class
C) when a member variable of class A makes use of an object of type class B
D) the collection of a parent class along with its descendants

Ques 38) :-

Which of the following is a benefit of inheritance?


A) Inheritance allows variables to be collected in arrays.
B) Inheritance leads to the duplication of code in multiple classes.
C) Inheritance allows two or more classes to share some common features yet differentiate
themselves on others.
D) Inheritance allows Visual Basic applications to directly connect to databases.

Ques 39) :-
Given the following piece of code:
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 );
}}

What will be the result?

A) 0 6 1 7 2 8 3 8
B) 0 6 1 7 2 8 3 9
C) 0 5 1 5 2 5 3 5
D) compilation fails
LNCT GROUP OF COLLEGES JAVA MCQ QUESTION

Ques 40) :-
Using which keyword we can access 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


Ques 41)
If a variable is declared final, it must include …………………. value.

A) integer

B) no

C) initial

D) float
Ques 42) :-
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


Ques 43) :-
In java, string is a ………….

A) primitive data type

B) abstract data type

C) combination of boolean
D) None of the above
Ques 44) :-
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


Ques 45) :-
Each method in a java class must have a unique name .

A) Not necessary

B) True

C) False

D) None of the above

Ques 46) :- 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


Ques 47) :-
It is 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

Ques 48) :- ………………….. is the key to ………………

A) Serialization, persistence

B) Persistence, inheritance

C) Inheritance, object

D) Persistence, serialization

Ques 49) :- State true of false.

i) 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 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

Ques 50) :-

Which constructor creates an empty string buffer with the specified


capacity as length.

A. StringBuffer()

B. StringBuffer(String str)

C. StringBuffer(int capacity)

D. None of the above

You might also like