Explore topic-wise MCQs in Java Programming.

This section includes 25 Mcqs, each offering curated multiple-choice questions to sharpen your Java Programming knowledge and support exam preparation. Choose a topic below to get started.

1.

In the below code, which code fragment should be inserted at line 3 so that the output will be: 234pqr 234pqr ?

1. StringBuilder strbuilder = new StringBuilder("234");
2. String str = "234";
3. // insert code here
4. System.out.println(strbuilder + " " + str);

A. strbuilder .append( pqr ); str.concat( pqr );
B. strbuilder .append( pqr ); str1 = str1.concat( pqr );
C. strbuilder .append( pqr ); str.append( pqr );
D. strbuilder .concat( pqr ); str.append( pqr );
E. None of these
Answer» C. strbuilder .append( pqr ); str.append( pqr );
2.

What will s2 contain after following lines of code?

String str1 = "one";
String str2 = str1.concat("two")

A. onetwo
B. twoone
C. one
D. two
E. None of these
Answer» B. twoone
3.

What is the output of this program?

public class Result
{
public static void main(String args[])
{
String ch = "hello i love interview mania";
boolean var0;
var0 = ch.startsWith("hello");
System.out.println(var0);
}
}

A. Hello
B. hello
C. false
D. true
E. None of these
Answer» E. None of these
4.

What is the output of this program?

public class Result
{
public static void main(String args[])
{
String ch[] = {"p", "q", "p", "s", "t"};
for (int K = 0; K < ch.length; ++K)
for (int L = K + 1; L < ch.length; ++L)
if(ch[K].compareTo(ch[L]) == 0)
System.out.print(ch[L]);
}
}

A. p
B. q
C. r
D. s
E. t
Answer» B. q
5.

What is the output of this program?
public class Result 
{
public static void main(String args[])
{
String str1 = "Interview Mania";
String str2 = str1.substring(0 , 9);
System.out.println(str2);
}
}

A. Interview
B. Mania
C. Interview Mania
D. Inter
E. None of these
Answer» B. Mania
6.

What is the output of this program?

public class output
{
public static void main(String args[])
{
String str1 = "Intervzew Manza";
String str2 = str1.replace('z','i');
System.out.println(str2);
}
}

A. Intervzew
B. Manza
C. Interview Mania
D. IntervzewManza
E. None of these
Answer» D. IntervzewManza
7.

What is the output of this program?

public class Result
{
public static void main(String args[])
{
String str1 = "Interview";
String str2 = str1 + " Mania";
System.out.println(str2);
}
}

A. Interview Mania
B. Interview
C. Mania
D. Mania Interview
E. InterviewMania
Answer» B. Interview
8.

What is the output of this program?

public class Result
{
public static void main(String args[])
{
String str0 = " Interview mania ";
String str1 = str0.trim();
System.out.println(" ""+str1+" "");
}
}

A. " Interview mania "
B. "Interview mania"
C. Interview mania
D. Interviewmania
E. None of these
Answer» C. Interview mania
9.

What is the output of this program?

public class Result
{
public static void main(String args[])
{
String str1 = "Hello";
String str2 = new String(str1);
String str3 = "HELLO";
System.out.println(str1.equals(str2) + " " + str2.equals(str3));
}
}

A. false
B. true
C. true false
D. false true
E. None of these
Answer» D. false true
10.

What is the output of this program?

public class output
{
public static void main(String args[])
{
String str1 = "Hello i love interview mania";
String str2 = new String(str1);
System.out.println((str1 == str2) + " " + str1.equals(str2));
}
}

A. false true
B. true false
C. true
D. false
E. None of these
Answer» B. true false
11.

What is the string contained in s after following lines of code?
StringBuffer str new StringBuffer("ZMania");
str.deleteCharAt(0);

A. ZMania
B. Mania
C. ania
D. ZMani
E. None of these
Answer» C. ania
12.

What is the output of this program?
public class Result 
{
public static void main(String args[])
{ String str = "Interview Mania";
int K = str.indexOf('v');
int L = str.lastIndexOf('i');
System.out.print(K + " " + L);

}
}

A. 13 5
B. 3 5
C. 5 13
D. 5 3
E. None of these
Answer» D. 5 3
13.

What is the output of this program?

public class String_Example
{
public static void main(String args[])
{
char chars[] = {'I', 'L', 'U'};
String obj = new String(chars);
String obj1 = "InterviewMania";
int len0 = obj1.length();
int len1 = obj.length();
System.out.println(len0 + " " + len1);
}
}

A. 14 3
B. 3 14
C. 41 3
D. 3 41
E. None of these
Answer» B. 3 14
14.

What is the output of this program?

public class Result
{
public static void main(String[]args)
{
String[] array = { "for", "tea", "too" };
String num= (array.length > 2) ? array[2]: null;
}
}

A. The variable num is set to elements[0].
B. The variable num is set to null
C. An exception is thrown at run time
D. Compilation error
E. None of these
Answer» B. The variable num is set to null
15.

What is the output of this program?

public class stringClass_Example
{
public static void main(String args[])
{
String str = "Interview";
String str1 = "Mania";
String str2 = "Interview";
System.out.println(str.equals(str1) + " " + str.equals(str2));
}
}

A. true true
B. false false
C. false true
D. true false
E. None of these
Answer» D. true false
16.

What is the output of this program?

public class string_Example
{
public static void main(String args[])
{
String obj = "I " + "like " + "Interview Mania ";
System.out.println(obj);
}
}

A. Interview Mania
B. InterviewMania
C. i like
D. like Interview
E. I like Interview Mania
Answer» F.
17.

What is the output of this program?

public class stringClass_Example
{
public static void main(String args[])
{
String str = "Interview";
String str1 = "Mania";
String str2 = str;
str2 = " Mania";
System.out.println(str + " " + str2);
}
}

A. Mania
B. Interview
C. Mania Interview
D. Interview Mania
E. None of these
Answer» E. None of these
18.

What is the output of this program?

public class String_Example
{
public static void main(String args[])
{
int ascii[] = { 80, 75, 70, 90};
String obj = new String(ascii, 1, 3);
System.out.println(obj);
}
}

A. KFZ
B. KZF
C. FKZ
D. ZFK
E. ZKF
Answer» B. KZF
19.

What is the output of this program?

public class String_Example
{
public static void main(String args[])
{
char chars[] = {'I', 'L', 'U'};
String obj = new String(chars);
System.out.println(obj);
}
}

A. ILU
B. ULI
C. IUL
D. LUI
E. LIU
Answer» B. ULI
20.

What is the output of this program?

public class stringClass_Example
{
public static void main(String args[])
{
String str = "Interview";
String str1 = "Mania";
String str2 = str;
str2 = " world";
System.out.println(str + " " + str2);
}
}

A. Interview
B. Mania
C. Interview Mania
D. Mania Interview
E. None of these
Answer» D. Mania Interview
21.

What is the output of this program?

public class string_class_Example
{
public static void main(String args[])
{
String obj = "I LIKE INTERVIEW MANIA";
System.out.println(obj.length());
}
}

A. 12
B. 22
C. 32
D. 42
E. 52
Answer» C. 32
22.

What is the output of this program?

public class string_Example
{
public static void main(String args[])
{
String obj = "I LIKE INTERVIEW MANIA";
System.out.println(obj.charAt(10));
}
}

A. I
B. L
C. K
D. N
E. E
Answer» F.
23.

What will s2 contain after following lines of code?

A. onetwo
B. twoone
C. one
D. two
E. None of these
Answer» B. twoone
24.

In the below code, which code fragment should be inserted at line 3 so that the output will be: 234pqr 234pqr ?

A. strbuilder .append( pqr ); str.concat( pqr );
B. strbuilder .append( pqr ); str1 = str1.concat( pqr );
C. strbuilder .append( pqr ); str.append( pqr );
D. strbuilder .concat( pqr ); str.append( pqr );
E. None of these
Answer» C. strbuilder .append( pqr ); str.append( pqr );
25.

What is the string contained in s after following lines of code?

A. ZMania
B. Mania
C. ania
D. ZMani
E. None of these
Answer» C. ania