Evaluating String Expressions

Help Questions

AP Computer Science A › Evaluating String Expressions

Questions 1 - 10
1

What is the output of the following code?

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

4 foo 253-2

4 foo 251

4 foo 73-2

4 foo 8

12 foo

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.

2

What is the output of the following code?

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

4 foo 253-2

4 foo 251

4 foo 73-2

4 foo 8

12 foo

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.

3

What is the output of the following code?

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

4 foo 253-2

4 foo 251

4 foo 73-2

4 foo 8

12 foo

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.

4

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?

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!

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.

5

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?

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!

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.

6

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?

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!

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.

7

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?

No output. An exception is thrown of type ClassCastException

two

"2"

2

A compiler error will be thrown

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.

8

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?

No output. An exception is thrown of type ClassCastException

two

"2"

2

A compiler error will be thrown

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.

9

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?

No output. An exception is thrown of type ClassCastException

two

"2"

2

A compiler error will be thrown

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.

10

What is the best way to print out a String variable into a sentence in Swift (iOS)?

var str = "Hello"

println("This string says \(str)")

var str = "Hello"

println("This string says Hello")

var str = "Hello"

println("This string says " + str)

var str = "Hello"

println("This string says str")

Explanation

The \(str) is the best way in Swift. This notation allows for a value of a variable to be input into the sentence. The "+" notation is also correct but is not the best way for Swift. The other two answers print out the words "str" and "Hello" instead of the value of the variable.

Page 1 of 3
Return to subject