Docsity

COMP110-003 Fall 2008 Review: Strings, Branching, Loops, Assignments of Computer Science

Toggle sidebar
bg1
COMP110-003 (Fall 2008) Review: Strings, Branching, Loops Name: ______________________
1. Suppose you have the variable myString of type String with data โ€œGo Tar Heels!โ€ Write out the data stored
in myString and place the index of each character below the string. See p. 83 of 4th edition or p. 77 of 5th
edition if you have questions.
2. Give the return type and data of the following method calls on myString (defined in question 2).
a. myString.length();
b. myString.trim();
c. myString.substring(7);
d. myString.charAt(3);
e. myString.lastIndexOf(โ€œ โ€);
f. myString.substring(myString.indexOf(โ€œ โ€) + 1,
myString.lastIndexOf(โ€œ โ€));
3. What is wrong with this code?
if (myString == โ€œGo Tar Heels!โ€)
System.out.println(โ€œYay!โ€);
4. Consider the following code.
if ((x > (y + 1)) && (x > -2) && (x < 1))
System.out.println(โ€œAโ€);
else if ((x > 0) && ((x < y) || (x < -1)))
System.out.println(โ€œBโ€);
else
System.out.println(โ€œCโ€);
What is the output if x = 0 and y = -4? __________
pf2

Partial preview of the text

Download COMP110-003 Fall 2008 Review: Strings, Branching, Loops and more Assignments Computer Science in PDF only on Docsity!

COMP110-003 (Fall 2008) Review: Strings, Branching, Loops Name: ______________________

1. Suppose you have the variable myString of type String with data โ€œGo Tar Heels!โ€ Write out the data stored

in myString and place the index of each character below the string. See p. 83 of 4th^ edition or p. 77 of 5th

edition if you have questions.

2. Give the return type and data of the following method calls on myString (defined in question 2).

a. myString.length();

b. myString.trim();

c. myString.substring(7);

d. myString.charAt(3);

e. myString.lastIndexOf(โ€œ โ€);

f. myString.substring(myString.indexOf(โ€œ โ€) + 1,

myString.lastIndexOf(โ€œ โ€));

3. What is wrong with this code?

if (myString == โ€œGo Tar Heels!โ€)

System.out.println(โ€œYay!โ€);

4. Consider the following code.

if ((x > (y + 1)) && (x > -2) && (x < 1)) System.out.println(โ€œAโ€); else if ((x > 0) && ((x < y) || (x < -1))) System.out.println(โ€œBโ€); else System.out.println(โ€œCโ€);

What is the output if x = 0 and y = -4? __________

What is the output if x = 2 and y = 1? __________

5. Write down whether the Boolean expression (x != 1) || (x == y) is true or false if the values of x and y are:

a. x = 1; y = 1; __________

b. x = 1; y = 0; __________

6. What is the value of temp at the end of the loop for the following input? __________

System.out.print("Please input an int: "); int input = kb.nextInt(); int temp = 0; while (input > 0) { if (input > 5) temp++; System.out.print("Please input an int: "); input = kb.nextInt(); }

What is a better name for temp (think about what the program is doing)? __________

7. What is the value of temp at the end of the loop for the following input? __________

System.out.print("Please input an int: "); input = kb.nextInt(); int temp = input; for (int i = 0; i < 7; i++) { if (input > temp) temp = input; System.out.print("Please input an int: "); input = kb.nextInt(); }

What is a better name for temp (think about what the program is doing)? __________

8. What is the value of temp at the end of the loop for the following input? __________

int temp = 0; do { System.out.print("Please input an int: "); input = kb.nextInt(); temp += input; } while (input > 0);

What is a better name for temp (think about what the program is doing)? __________