0% found this document useful (0 votes)
30 views10 pages

MCQ Strings

The document contains a series of programming questions and answers related to Java, specifically focusing on string manipulation, class behavior, and method functionality. Each question presents a code snippet or concept, followed by multiple choice answers, with the correct answers indicated. The questions cover topics such as string immutability, method outputs, and class constructors.

Uploaded by

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

MCQ Strings

The document contains a series of programming questions and answers related to Java, specifically focusing on string manipulation, class behavior, and method functionality. Each question presents a code snippet or concept, followed by multiple choice answers, with the correct answers indicated. The questions cover topics such as string immutability, method outputs, and class constructors.

Uploaded by

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

Ques - 1

------------
3. public class Theory {
4. public static void main(String[] args) {
5. String s1 = "abc";
6. String s2 = s1;
7. s1 += "d";
8. System.out.println(s1 + " " + s2 + " " + (s1==s2));
9.
10. StringBuffer sb1 = new StringBuffer("abc");
11. StringBuffer sb2 = sb1;
12. sb1.append("d");
13. System.out.println(sb1 + " " + sb2 + " " + (sb1==sb2));
14. }
15. }
Which are true? (Choose all that apply.)
A. Compilation fails
B. The first line of output is abc abc true
C. The first line of output is abc abc false
D. The first line of output is abcd abc false
E. The second line of output is abcd abc false
F. The second line of output is abcd abcd true
G. The second line of output is abcd abcd false
Ans : D,F
===================================================================================
================
Ques - 2
-------------
Given:
3. import java.util.*;
4. public class Looking {
5. public static void main(String[] args) {
6. String input = "1 2 a 3 45 6";
7. Scanner sc = new Scanner(input);
8. int x = 0;
9. do {
10. x = sc.nextInt();
11. System.out.print(x + " ");
12. } while (x!=0);
13. }
14. }
What is the result?
A. 1 2
B. 1 2 3 45 6
C. 1 2 3 4 5 6
D. 1 2 a 3 45 6
E. Compilation fails
F. 1 2 followed by an exception
Ans : F
===================================================================================
================
Ques - 3
---------
Given:
public class Banana {
public static void main(String[] args) {
String in = "1 a2 b 3 c4d 5e";
String[] chunks = in.split(args[0])
System.out.println("count " + chunks.length);
for(String s : chunks)
System.out.print(">" + s + "< ");
}
}
And two invocations:
java Banana " "
java Banana "\d"
What is the result? (Choose all that apply.)
A. In both cases, the count will be 5
B. In both cases, the count will be 6
C. In one case, the count will be 5, and in the other case, 6
D. Banana cannot be invoked because it will not compile
E. At least one of the invocations will throw an exception
Ans : B
===================================================================================
================
Ques - 4
------------
Given:
public class Legos {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder(8);
System.out.print(sb.length() + " " + sb + " ");
sb.insert(0, "abcdef");
sb.append("789");
System.out.println(sb.length() + " " + sb);
}
}
What is the result?
A. 0 8 abcdef78
B. 0 8 789abcde
C. 0 9 abcdef789
D. 0 9 789abcdef
E. Compilations fails
F. 0, followed by an exception
Ans : C
===================================================================================
================
Ques - 5
------------
Given:
3. public class Theory {
4. public static void main(String[] args) {
5. String s1 = "abc";
6. String s2 = s1;
7. s1 += "d";
8. System.out.println(s1 + " " + s2 + " " + (s1==s2));
9.
10. StringBuffer sb1 = new StringBuffer("abc");
11. StringBuffer sb2 = sb1;
12. sb1.append("d");
13. System.out.println(sb1 + " " + sb2 + " " + (sb1==sb2));
14. }
15. }
Which are true? (Choose all that apply.)
A. Compilation fails
B. The first line of output is abc abc true
C. The first line of output is abc abc false
D. The first line of output is abcd abc false
E. The second line of output is abcd abc false
F. The second line of output is abcd abcd true
G. The second line of output is abcd abcd false
Ans : D,F
===================================================================================
================
Ques - 6
------------
Given:
public class Stone {
public static void main(String[] args) {
String s = "abc";
System.out.println(">" + doStuff(s) + "<");
}
static String doStuff(String s) {
s = s.concat(" ef h ");
return s.trim();
}
}
What is the result?
A. >abcefh<
B. >efhabc<
C. >abc ef h<
D. \>>ef h abc<
E. >abc ef h <
Ans :C
===================================================================================
================
Ques - 7
------------
Which of the following operators cannot have an operand of type String?
Select the two correct answers.
(a) +
(b) -
(c) +=
(d) .
(e) &
Ans :B,E
===================================================================================
================
Ques - 8
------------
Which expression will extract the substring "kap", given the following declaration:
String str = "kakapo";
Select the one correct answer.
(a) str.substring(2, 2)
(b) str.substring(2, 3)
(c) str.substring(2, 4)
(d) str.substring(2, 5)
(e) str.substring(3, 3)
Ans :D
===================================================================================
================
Ques - 9
------------
What will be the result of attempting to compile and run the following code?
class MyClass {
public static void main(String[] args) {
String str1 = "str1";
String str2 = "str2";
String str3 = "str3";
str1.concat(str2);
System.out.println(str3.concat(str1));
}
}
Select the one correct answer.
(a) The code will fail to compile because the expression str3.concat(str1) will not
result in a valid argument for the println() method.
(b) The program will print str3str1str2, when run.
(c) The program will print str3, when run.
(d) The program will print str3str1, when run.
(e) The program will print str3str2, when run
Ans : D
===================================================================================
================
Ques - 10
------------
Which statement about the trim() method of the String class is true?
Select the one correct answer.
(a) It returns a string where the leading white space of the original string has
been removed.
(b) It returns a string where the trailing white space of the original string has
been removed.
(c) It returns a string where both the leading and trailing white space of the
original string has been removed.
(d) It returns a string where all the white space of the original string has been
removed.
(e) None of the above
Ans : C
===================================================================================
================
Ques - 11
------------
Which statements are true?
Select the two correct answers.
(a) String objects are immutable.
(b) Subclasses of the String class can be mutable.(NO SUBCLASS BECAUSE CLASS IS
FINAL)
(c) All wrapper classes are declared final.
(d) All objects have a public method named clone.
(e) The expression ((new StringBuilder()) instanceof String) is always true.
Ans : A, C
===================================================================================
================
Ques - 12
------------
Which of these expressions are legal?
Select the four correct answers.
(a) "co".concat("ol")
(b) ("co" + "ol")
(c) ('c' + 'o' + 'o' + 'l')
(d) ("co" + new String('o' + 'l')) (NO CHAR[] CONSTRACTER)
(e) ("co" + new String("co"))
Ans :A, B, C, E
===================================================================================
================
Ques - 13
------------
What will be the result of attempting to compile and run the following program?
public class RefEq {
public static void main(String[] args) {
String s = "ab" + "12";
String t = "ab" + 12;
String u = new String("ab12");
System.out.println((s==t) + " " + (s==u));
}
}
Select the one correct answer.
(a) The program will fail to compile.
(b) The program will print false false, when run.
(c) The program will print false true, when run.
(d) The program will print true false, when run.
(e) The program will print true true, when run.
Ans : D
===================================================================================
================
Ques - 14
------------
Which of these parameter lists can be found in a constructor of the String class?
Select the three correct answers.
(a) ()
(b) (int capacity)
(c) (char[] data)
(d) (String str)
Ans : A,C,D
===================================================================================
================
Ques - 15
------------
Which method is not defined in the String class?
Select the one correct answer.
(a) trim()
(b) length()
(c) concat(String)
(d) hashCode()
(e) reverse()
Ans : E
===================================================================================
================
Ques - 16
------------
Which statement about the charAt() method of the String class is true?
Select the one correct answer.
(a) The charAt() method takes a char value as an argument.
(b) The charAt() method returns a Character object.
(c) The expression ("abcdef").charAt(3) is illegal.
(d) The expression "abcdef".charAt(3) evaluates to the character 'd'.
(e) The index of the first character is 1.
Ans : D
===================================================================================
================
Ques - 17
------------
Which expression will evaluate to true?
Select the one correct answer.
(a) "hello: there!".equals("hello there")
(b) "HELLO THERE".equals("hello there")
(c) ("hello".concat("there")).equals("hello there")
(d) "Hello There".compareTo("hello there") == 0
(e) "Hello there".toLowerCase().equals("hello there")
Ans : E
===================================================================================
================
Ques - 18
------------
What will the following program print when run?
public class Search {
public static void main(String[] args) {
String s = "Contentment!";
int middle = s.length()/2;
String nt = s.substring(middle-1, middle+1);
System.out.println(s.lastIndexOf(nt, middle));
}
}
Select the one correct answer.
(a) 2
(b) 4
(c) 5
(d) 7
(e) 9
(f) 11
Ans : C
===================================================================================
================
Ques - 19
------------
What will the following program print when run?
public class Uppity {
public static void main(String[] args) {
String str1 = "lower", str2 = "LOWER", str3 = "UPPER";
str1.toUpperCase();
str1.replace("LOWER","UPPER");
System.out.println((str1.equals(str2)) + " " + (str1.equals(str3)));
}
}
Select the one correct answer.
(a) The program will print false true.
(b) The program will print false false.
(c) The program will print true false.
(d) The program will print true true.
(e) The program will fail to compile.
(f) The program will compile, but throw an exception at runtime.
Ans :B
===================================================================================
================
Ques - 20
------------
What will the following program print when run?
public class FunCharSeq {
private static void putO(String s1) {
s1 = s1.trim();
s1 += "O";
}
public static void main(String[] args) {
String s1 = " W ";
putO(s1);
s1.concat("W");
System.out.println("|" + s1 + "|");
}
}
Select the one correct answer.
(a) |WOW|
(b) | WW |
(c) | WO |
(d) | W |
(e) The program will fail to compile.
(f) The program will compile, but throw an exception at runtime.
Ans : D
===================================================================================
================
Ques - 21
------------
What will be the result of attempting to compile and run the following program?
public class MyClass {
public static void main(String[] args) {
String s = "hello";
StringBuilder sb = new StringBuilder(s);
sb.reverse();
if (s == sb) System.out.println("a");
if (s.equals(sb)) System.out.println("b");
if (sb.equals(s)) System.out.println("c");
}
}
Select the one correct answer.
(a) The code will fail to compile because the constructor of the String class is
not called properly.
(b) The code will fail to compile because the expression (s == sb) is illegal.
(c) The code will fail to compile because the expression (s.equals(sb)) is illegal.
(d) The program will print c, when run.
(e) The program will throw a ClassCastException, when run
Ans : B
===================================================================================
================
Ques - 22
----------
What will be the result of attempting to compile and run the following program?
public class MyClass {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("have a nice day");
sb.setLength(6);
System.out.println(sb);
}
}
Select the one correct answer.
(a) The code will fail to compile because there is no method named setLength in
the StringBuilder class.
(b) The code will fail to compile because the StringBuilder reference sb is not a
legal argument to the println() method.
(c) The program will throw a StringIndexOutOfBoundsException, when run.
(d) The program will print have a nice day, when run.
(e) The program will print have a, when run.
(f) The program will print ce day, when run
ANs : E
===================================================================================
================
Ques - 23
------------
Which of these parameter lists can be found in a constructor of the StringBuilder
class?
Select the three correct answers.
(a) ()
(b) (int capacity)
(c) (char[] data)
(d) (String str)
Ans : C
===================================================================================
================
Ques - 24
------------
Which method is not defined in the StringBuilder class?
Select the one correct answer.
(a) trim()
(b) length()
(c) append(String)
(d) reverse()
(e) setLength(int)
Ans : A
===================================================================================
================
Ques - 25
------------
What will be the result of attempting to compile and run the following program?
public class StringMethods {
public static void main(String[] args) {
String str = new String("eeny");
str.concat(" meeny");
StringBuilder strBuilder = new StringBuilder(" miny");
strBuilder.append(" mo");
System.out.println(str + strBuilder);
}
}
Select the one correct answer.
(a) The program will fail to compile.
(b) The program will print eeny meeny miny mo, when run.
(c) The program will print meeny miny mo, when run.
(d) The program will print eeny miny mo, when run.
(e) The program will print eeny meeny miny, when run.
Ans : D
===================================================================================
================
Ques - 26
------------
What will the following program print when run?
public class PeskyCharSeq {
public static void main (String[] args) {
StringBuilder sb1 = new StringBuilder("WOW");
StringBuilder sb2 = new StringBuilder(sb1);
System.out.println((sb1==sb2) + " " + sb1.equals(sb2));
}
}
Select the one correct answer.
(a) The program will print false true.
(b) The program will print false false.
(c) The program will print true false.
(d) The program will print true true.
(e) The program will fail to compile.
(f) The program will compile, but throws an exception at runtime.
Ans : B
===================================================================================
================
Ques - 27
------------
What will the following program print when run?
public class MoreCharSeq {
public static void main (String[] args) {
String s1 = "WOW";
StringBuilder s2 = new StringBuilder(s1);
String s3 = new String(s2);
System.out.println((s1.hashCode() == s2.hashCode()) + " " +
(s1.hashCode() == s3.hashCode()));
}
}
Select the one correct answer.
(a) The program will print false true.
(b) The program will print false false.
(c) The program will print true false.
(d) The program will print true true.
(e) The program will fail to compile.
(f) The program will compile, but throw an exception at runtime
Ans : B
===================================================================================
================
Ques - 28
------------
What will the following program print when run?
public class CharSeq {
public static void main (String[] args) {
String cs1 = "JAVA";
StringBuilder cs2 = new StringBuilder(cs1);
System.out.println(cs1.compareTo(cs2) == cs2.compareTo(cs1));
}
}
Select the one correct answer.
(a) The program will print false.
(b) The program will print true.
(c) The program will fail to compile.
(d) The program will compile, but throw an exception at runtime
Ans : C
===================================================================================
================
Ques - 29
------------
What will the following program print when run?
public class Appendage {
private static void putO(StringBuilder s1) {
s1 = s1.append("O");
}
public static void main(String[] args) {
StringBuilder s1 = new StringBuilder("W");
putO(s1);
s1.append("W!");
System.out.println(s1);
}
}
Select the one correct answer.
(a) The program will print WW!.
(b) The program will print WOW!.
(c) The program will print W.
(d) The program will fail to compile.
(e) The program will compile, but throw an exception at runtime.
Ans : B
===================================================================================
================
Ques - 30
------------
What will the following program print when run?
public class Chains {
private static StringBuilder putO(StringBuilder s1) {
s1.append("O");
return s1;
}
public static void main(String[] args) {
StringBuilder s1 = new StringBuilder("W");
boolean status = putO(s1).append("W!").toString().compareTo("WOW!") != 0;
System.out.println(s1 + " " + status);
}
}
Select the one correct answer.
(a) The program will print WOW! false.
(b) The program will print WOW! true.
(c) The program will print WW! true.
(d) The program will fail to compile.
(e) The program will compile, but throw an exception at runtime.
Ans : A

You might also like