Midterm Practice Problems

CS31: Introduction to Computer Science I

Midterm Practice Problems

TA: Brian Choi (schoi@cs.ucla.edu)

Section Webpage: http://www.cs.ucla.edu/~schoi/cs31

Spring 2011

ENJOY!

Problem 1

True False Variable names can begin with an alphabet or a digit.

True False A string cannot be empty.

True False A character cannot be empty.

True False An array can have a size of 0.

True False int m = 5.6; won’t compile because types do not match.

True False int n = 11 / 5; will create n and initialize it with the value of 2.

True False An array index begins with 0.

True False for (int i = 0; i <= 49; i++) will run the loop 50 times

(assuming i is not modified within the loop).

True False If there is a function that does not require any parameters, you can omit parentheses when calling it.

True False Constant variables can be modified only in the main() function.

True False int x = ‘0’; sets x to an integer 0.

True False int x = 0; sets x to an integer 0.

True False int x = 0.0; sets x to an integer 0.

True False int x = 0.5; sets x to an integer 0.

True True You will ace the exam.

Problem 2

What does the following code do, in a brief English sentence?

int mystery(int k)

{

int i = 0;

if (k < 0)

return -1;

while (k > 0)

{

if (k % 3 == 0)

i++;

k--;

}

return i;

}

Copyright Brian Choi 2011. All Rights Reserved. Week 5, Page 1/5

CS31: Introduction to Computer Science I

Problem 3

Spring 2011

What happens if you compile and run the following code? For each case, choose one of the options and answer the question that belongs to your choice.

int k[10] = {2, -1, 2, 7, 3, -2, -1, 1, -7, -3}; int steps = 0; int j; cin >> j; // user enters a digit while (j >= 0 && j < 10)

{

j += k[j];

steps++;

} cout << steps << endl;

Suppose user enters

9

.

1. It successfully compiles and prints something.

Q: What does it print?

2. A compile error occurs.

Q: Explain the error.

3. A runtime error occurs.

Q: Explain the error.

Now suppose user enters 2 .

1. It successfully compiles and prints something.

Q: What does it print?

2. A compile error occurs.

Q: Explain the error.

3. A runtime error occurs.

Q: Explain the error.

Copyright Brian Choi 2011. All Rights Reserved. Week 5, Page 2/5

CS31: Introduction to Computer Science I

Problem 4

Spring 2011

Your friend at U$C is also taking a CS31-equivalent course at his institution. For his project, he has to write a function that takes in an integer and prints a triangle based on the number. Here are the example runs of the function: tri(3) tri(1) tri(5)

* * *

*** ***

***** *****

*******

*********

(There is no space in front of the last line.)

He spends three hours on the project, but he can't get the function working. He ended up submitting the following code, and wants you to have a look and see what’s wrong:

0 void tri(int num)

1 {

2 // error if num is not a positive number

3 if (num > 0)

4 return -1;

5

6 int i = 0;

7

8 while (i < num)

9 {

10 // Add spaces in front.

11 for (int j = 0; j < num - i; j++)

12 {

13 cout << " ";

14 }

15

16 // Draw the stars.

17 for (int j = 0; j < i; j++)

18 {

19 cout << "*";

20 }

21

22 cout << endl;

23 i++;

24 }

25

26 return 0;

27 }

Unfortunately you’re in a library, so don’t have access to Visual C++. But you should be good enough to debug the code just by reading. Find errors for this poor Trojan and correct them so that it works. Clearly indicate the error and write the replacement code. Do not add a new line of code, though.

Copyright Brian Choi 2011. All Rights Reserved. Week 5, Page 3/5

CS31: Introduction to Computer Science I

Problem 5

Predict the output of the following code.

void mystery2(int &a, int b, int &c)

{

a = b + c;

b = a + c;

c = a + b;

} int main()

{

int a, b, c;

a = 1;

b = 2;

c = 3;

mystery2(b, c, a);

cout << a << " " << b << " " << c << endl;

}

Problem 6

Write a function rotate that takes in a string s and “rotates” the string to the right k times.

rotate(“abcde”, 1) returns “ e abcd” rotate(“abcde”, 2) returns “ de abc” rotate(“abcde”, 3) returns “ cde ab” rotate(“abcde”, 7) returns “ de abc”

Do not rotate the string if k is negative.

Spring 2011

Copyright Brian Choi 2011. All Rights Reserved. Week 5, Page 4/5

CS31: Introduction to Computer Science I

Problem 7

Spring 2011

Given an array of words, and I want to find all the “Hebrew” words. “Hebrew” words, in our definition, are words without any vowels. For example: “xyzzy” could be considered a Hebrew word, while “ball” is not.

The only vowels in English, in case you don’t remember, are ‘a’, ‘e’, ‘i’, ‘o’, ‘u’. Assume all the words in the array consist only of lowercase letters (i.e. no uppercase letters, no spaces, no other symbols).

a) Write the function isHebrew that takes in a word (of type string ) that consists only of lowercase letters and returns true has the “Hebrew” nature, false otherwise.

bool isHebrew(string word)

{ b) Write the function hebrew that takes in an array of words (array of strings), keeps only candidate

“Hebrew” words in the array, and returns the new size of the array. For example: string words[4] = {“xyz”, “ucla”, “sky”, “this”}; int newSize = hebrew(words);

// newSize should be 2, and words should be {“xyz”, “sky”} or {“sky”, “xyz”}.

// words will have two more elements, but we do not care what values they carry.

// The first two elements are what matter.

Let n be the size of the array. Return -1 if n is negative.

int hebrew(string words[], int n)

{

Copyright Brian Choi 2011. All Rights Reserved. Week 5, Page 5/5