The pages of this web site are not formatted to conserve paper, but my new book (ISBN: 0-9745862-0-X) is formatted to save paper, save your printer cartridge, save a loose-leaf binder, and save money. If you prefer to work my exams from printed pages, then give your printer a rest and buy my new book.
Today, you can find my book at amazon.com.
If you have any questions or comments concerning my mock exams or my book, then please send an e-mail to me at scjpexam2000@yahoo.com.
Which of the following are not methods of the java.lang.String class?
| a. | append |
| b. | concat |
| c. | delete |
| d. | insert |
| e. | replace |
| f. | substring |
| g. | valueOf |
Which of these methods modify the contents of the String instance on which it is invoked?
| a. | append |
| b. | concat |
| c. | delete |
| d. | insert |
| e. | replace |
| f. | substring |
| g. | valueOf |
| h. | None of the above. |
Which of these methods is a static member of the java.lang.String class?
| a. | append |
| b. | concat |
| c. | delete |
| d. | insert |
| e. | replace |
| f. | substring |
| g. | valueOf |
| h. | None of the above. |
class MWC106 {
static void m1(String s) {
s = s.trim(); s = s.concat("D");
}
public static void main(String[] s) {
String s1 = "A", s2 = " B ", s3 = "C";
m1(s2);
System.out.print(s1 + s2 + s3);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: ABC |
| b. | Prints: A B C |
| c. | Prints: ABCD |
| d. | Prints: ABDC |
| e. | Prints: A B CD |
| f. | Prints: A B DC |
| g. | Compile-time error |
| h. | Run-time error |
| i. | None of the above |
class MWC107 {
public static void main(String[] s) {
String s1 = "A", s2 = " B ", s3 = "C";
s2.trim(); s3.concat("D");
System.out.print(s1 + s2 + s3);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: ABC |
| b. | Prints: A B C |
| c. | Prints: ABCD |
| d. | Prints: ABDC |
| e. | Prints: A B CD |
| f. | Prints: A B DC |
| g. | Compile-time error |
| h. | Run-time error |
| i. | None of the above |
class MWC108 {
public static void main(String[] s) {
String s1 = "A", s2 = " B ", s3 = "C";
s2.trim(); s3.append("D");
System.out.print(s1 + s2 + s3);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: ABC |
| b. | Prints: A B C |
| c. | Prints: ABCD |
| d. | Prints: ABDC |
| e. | Prints: A B CD |
| f. | Prints: A B DC |
| g. | Compile-time error |
| h. | Run-time error |
| i. | None of the above |
class MWC110 {
static void m1(String s1, String s2) {
s1.insert(1,"D"); s1 = s1 + s2;
}
public static void main(String[] s) {
String s1 = "A", s2 = "B";
m1(s1,s2);
System.out.print(s1 + s2);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: AB |
| b. | Prints: ABB |
| c. | Prints: ADB |
| d. | Prints: ADBB |
| e. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |
class MWC111 {
static void m1(String s1) {
s1.replace('A','Y'); System.out.print(s1);
}
static void m2(String s1) {
s1 = s1.replace('A','Z'); System.out.print(s1);
}
public static void main(String[] s) {
String s1 = "A"; m1(s1); m2(s1);
System.out.print(s1);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: AAA |
| b. | Prints: YZA |
| c. | Prints: YAA |
| d. | Prints: AZA |
| e. | Prints: AZZ |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
class MWC112 {
static void m1(String s1) {
s1 = s1.toUpperCase(); System.out.print(s1);
}
static void m2(String s1) {
s1.toLowerCase(); System.out.print(s1);
}
public static void main(String[] s) {
String s1 = "Ab";
m1(s1); m2(s1);
System.out.print(s1);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: AbAbAb |
| b. | Prints: ABabab |
| c. | Prints: ABabAb |
| d. | Prints: ABAbAb |
| e. | Prints: Ababab |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
class MWC113 {
public static void main(String[] s) {
String s1 = "1", s2 = "2", s3 = s1 + s2;
s1 += s2; s3 += s1;
System.out.println(s3);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 3 |
| b. | Prints: 6 |
| c. | Prints: 33 |
| d. | Prints: 1212 |
| e. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |
class MWC114 {
public static void main(String[] s) {
String s1 = new String("ABCDEFG"), s2 = new String("EFGHIJ");
String s3 = s1.substring(4,7), s4 = s2.substring(0,3);
System.out.println((s3 == s4) + "," + (s3 + s4).equals(s4 + s3));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: false,false |
| b. | Prints: false,true |
| c. | Prints: true,false |
| d. | Prints: true,true |
| e. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |
class MWC115 {
public static void main(String[] s) {
String s1 = new String("ABCDEFG"), s2 = s1.substring(0,3);
String s3 = s1.substring(4,6); char c1 = s1.charAt(3);
System.out.println(s2.concat(String.valueOf(c1)).concat(s3));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: "ABCDEFG" |
| b. | Prints: "ABCEFG" |
| c. | Prints: "ABCDDEFG" |
| d. | Prints: "ABCDEF" |
| e. | Prints: "ABCEF" |
| f. | Prints: "ABCDDEF" |
| g. | Compile-time error |
| h. | Run-time error |
| i. | None of the above |
class MWC118 {
public static void main(String[] args) {
byte[] b = {'a', 'b', 'c'};
char[] c = {'a', 'b', 'c'};
String s = "abc";
StringBuffer sb = new StringBuffer("abc");
byte b2 = 'a';
char c2 = 'a';
String s1 = new String(b); // 1
String s2 = new String(c); // 2
String s3 = new String(s); // 3
String s4 = new String(sb); // 4
String s5 = new String(b2); // 5
String s6 = new String(c2); // 6
}}
Compile-time errors are generated at which lines?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | 5 |
| f. | 6 |
class MWC101 {
public static void main(String[] args) {
String s1 = "A", s2 = "a", s3 = "b";
s1.toLowerCase(); s3.replace('b','a');
System.out.print((s1.equals(s2)) + "," + (s2.equals(s3)));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: false,false |
| b. | Prints: false,true |
| c. | Prints: true,false |
| d. | Prints: true,true |
| e. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |
class MWC102 {
public static void main (String[] args) {
String s1 = "ABCDE";
System.out.print(s1.substring(1,2)+s1.substring(3));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: AABC |
| b. | Prints: ACDE |
| c. | Prints: ABABC |
| d. | Prints: ABCDE |
| e. | Prints: BABCD |
| f. | Prints: BDE |
| g. | Prints: BCABCD |
| h. | Prints: BCDE |
| i. | Compile-time error |
| j. | Run-time error |
| k. | None of the above |
class MWC104 {
public static void main (String[] args) {
char[] c = {'a','b','c','d'};
String s1 = new String(c);
boolean b = true;
for (int i = 0; i < s1.length; i++) {
b &= (c[i] == s1.charAt(i));
}
System.out.print(b);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: false |
| b. | Prints: true |
| c. | Compile-time error |
| d. | Run-time error |
| e. | None of the above |
class MWC109 {
public static void main(String args[]) {
String a = "A", b = "B", c = a+b, d = a+b;
System.out.print(((a+b)==(a+b)) + ",");
System.out.print((c==d) + ",");
System.out.print(c.equals(d));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: false,false,false |
| b. | Prints: false,false,true |
| c. | Prints: false,true,false |
| d. | Prints: false,true,true |
| e. | Prints: true,false,false |
| f. | Prints: true,false,true |
| g. | Prints: true,true,false |
| h. | Prints: true,true,true |
class MWC116 {
public static void main(String[] args) {
String s1 = " B C D E ";
System.out.print('A' + s1.trim() + 'F');
}}
What is the result of attempting to compile and run the program?
| a. | Prints: ABCDEF |
| b. | Prints: A B C D E F |
| c. | Prints: A BCDEF |
| d. | Prints: ABCDE F |
| e. | Prints: AB C D EF |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
class MWC117 {
public static void main (String[] args) {
System.out.print(String.valueOf(1) + String.valueOf(2));
String s1 = "S1";
String s2 = s1.toString();
System.out.print("," + (s1==s2));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 3,false |
| b. | Prints: 3,true |
| c. | Prints: 12,false |
| d. | Prints: 12,true |
| e. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |