AP Computer Science A : Evaluating String Expressions

Study concepts, example questions & explanations for AP Computer Science A

varsity tutors app store varsity tutors android store

Example Questions

Example Question #1 : Evaluating String Expressions

string str = "Hello"

char newvar = str[4];

What is the value of newvar?

Possible Answers:

'l'

'o'

" "

'e'

'H'

Correct answer:

'o'

Explanation:

The line, char newvar = str[4] is declaring a new variable which is of the type character. Further more, when it declares it, it is saying that it is the same as the character at the 4th location in the string above.

The value 4 actually represents the 5th value, since indices start at 0.

Therefore, at str[4] we have the character 'o'.

Example Question #2 : Evaluating String Expressions

String s = "abcdefghijklmnop";

String s2 = "";

for(int i = 0; i < s.length(); i += 2) {

     s2 += s.charAt(i+1);

     s2 += s.charAt(i);

}

What is the value of s2 at the end of the code above?

Possible Answers:

ponmlkjihgfedcba

adfgmbnciephjkloa

badcfehgjilknmpo

aabbccddeeffgghhiijjkkllmmnnoopp

adfgbnhciempjkloa

Correct answer:

badcfehgjilknmpo

Explanation:

String s = "abcdefghijklmnop";

String s2 = "";

for(int i = 0; i < s.length(); i += 2) {

     s2 += s.charAt(i+1);

     s2 += s.charAt(i);

}

The key logic of this, of course, occurs in the loop. This loop can be thought of as looking at each pair of characters (hence the use of i += 2 as your increment condition).  Thus, i will be 0,2,4, . . . For 0, you will first place character 1 on s2 then character 0. Then, for 2, you will place 3 followed by 2. The pattern will continue. This is the same as saying that you will first do b, then a, next d, then c, and so forth. Therefore, for this code, you are flipping each pair of letters, thus giving you the answer badcfehgjilknmpo.

Example Question #3 : Evaluating String Expressions

Consider the code below:

String s = "Logic!";

String s2 = "";

for(int i = 0; i < s.length(); i++) {

     if(i != 0) {

          s2 += " ";

     }

     s2 += s;

}

What is the value of s2 at the end of the code's execution?

Possible Answers:

Logic! Logic! Logic! Logic! Logic! Logic!

Logic! Logic! Logic! Logic! Logic! Logic!

L L L L L L o o o o o o g g g g g g i i i i i i c c c c c c ! ! ! ! ! !

LoGiC! LoGiC! LoGiC! LoGiC! LoGiC! LoGiC!

Logic! Logic! Logic!

Correct answer:

Logic! Logic! Logic! Logic! Logic! Logic!

Explanation:

for(int i = 0; i < s.length(); i++) {

     if(i != 0) {

          s2 += " ";

     }

     s2 += s;

}

Let us focus on the main for loop that is used in this code. Notice that the loop will run from 0 to the length of the string s. This string is 6 long. Therefore, the loop will run 6 times. The if statement adds a space to the string on every iteration except for the first. Therefore, the code copies the string 6 times, each copy being separated by a space.

Example Question #4 : Evaluating String Expressions

Consider the following code:

Q2

What is the output of the method call mystery("Green eggs and ham")?

Possible Answers:

G,r,e,e,n,e,g,g,s,a,n,d,h,a,m,

Green,eggs,and,ham,

ham,

Greeneggsandham,

Green eggs and ham

Correct answer:

Green,eggs,and,ham,

Explanation:

The method String.split() splits a String into an array of Strings separated according to the expression within the method argument. The expression String.split("\\s+") splits the String at every whitespace character. The "for" loop concatenates the elements of the String array together, separated by a comma. 

Example Question #5 : Evaluating String Expressions

What is the output of the following code?

System.out.println( (1+2) + (3-1) + new Integer(5));

Possible Answers:

10

Syntax error, will not run

325

45

36

Correct answer:

10

Explanation:

When evaluating a string expression with integers, arithmatic addition is performed if the integers are not separated by string Objects. Since all of the integers and operations in this expression are not separated by String objects, arithmatic operations are performed on the integers and the final airthmatic result is printed to the console.

Example Question #6 : Evaluating String Expressions

What is the output of the following code?

System.out.println((2*2) + " foo " + (3-1) + new Integer(5) + "3-2");

Possible Answers:

4 foo 8

12 foo

4 foo 253-2

4 foo 73-2

4 foo 251

Correct answer:

4 foo 253-2

Explanation:

The presence of the "foo" String in the middle of the expression makes it such that arithmatic operations are performed separately on the left and right sides of the String. It causes each plus sign to perform concatenation instead of addition between terms.

Example Question #7 : Evaluating String Expressions

Which of the following will cause the Java compiler to throw an error?

Possible Answers:

String s = null;

String s = new String("Hello");

String s = "Hello";

String s = "Hello" + 5;

String s = String("Hello");

Correct answer:

String s = String("Hello");

Explanation:

The answer is E. E will throw cause Java to throw an error.

The question tests the students' syntactical knoweldge of Java, as well as the meaning of Java throwing a compiler error. Before running a piece of Java code, it must be compiled. The compiler converts the code into a language the machine understands, but more importantly for this example it catches any syntactical errors that the user might have made. 

 

Answer A is correct, as you need the new keyword to initialize a String as an Object, so the line of code will throw an error. 

 

Answer B is incorrect, as Strings can also be initialized as a primitive type. 

 

Answer C is incorrect, as it is possible to give s a null reference.

 

Answer D is incorrect, as Java automatically casts the integer 5 to a string, effectivelly performing the following operation: "Hello" + "5". This is called implicit casting. This is a feature of the Java language, but for example, not of Python.

 

Answer E is incorrect, as Strings are also Objects in Java and can be initialized with the new keyword.

Example Question #8 : Evaluating String Expressions

Suppose you have the following lines of code:

Object integerObj = new Integer(2);

System.out.println((String) integerObj);

What will be the output resulting from running the lines of code?

Possible Answers:

A compiler error will be thrown

"2"

No output. An exception is thrown of type ClassCastException

two

2

Correct answer:

No output. An exception is thrown of type ClassCastException

Explanation:

You cannot cast an Object of type Integer to String. The tricky part here is that the compiler will not be able to catch this because it is integerObject has type Object. Had it been of type Integer, a compiler error would be thrown. Thus, the code fragment is allowed to run but an exception is thrown at runtime instead.

 

Additionally, the option "two" does not make any sense, because even if 2 was cast to a String, the output would be "2", and not "two." 

 

Also, the option ""2"" does not make sense because the quotes are not displayed.

Learning Tools by Varsity Tutors

Incompatible Browser

Please upgrade or download one of the following browsers to use Instant Tutoring: