Java MCQ by
Java MCQ by
Ques 1) :-
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:
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
}
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):-
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):-
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()
class Output {
System.out.print(Character.isUpperCase(a[2])); } }
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
class Output {
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
class Output {
int a = Character.MAX_VALUE;
System.out.print((char)a); }}
a) <
b) >
c) ?
d) $
class Output {
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
class Output {
try {
int x = i.compareTo(y);
System.out.print(x);
catch(ClassCastException e) {
System.out.print("Exception");
}}}
a) 0
b) 1
c) Exception
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
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.
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
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()); } }
Ques 36) :-
Ques 37) :-
Ques 38) :-
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 );
}}
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
A) integer
B) no
C) initial
D) float
Ques 42) :-
State true or false.
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
A) Not necessary
B) True
C) False
iv) x precede +
A) Not supported
B) False
C) True
A) Serialization, persistence
B) Persistence, inheritance
C) Inheritance, object
D) Persistence, serialization
Ques 50) :-
A. StringBuffer()
B. StringBuffer(String str)
C. StringBuffer(int capacity)