midterm practice problems 2
play

Midterm Practice Problems 2 This example midterm is extended from - PDF document

CS31: Introduction to Computer Science I Midterm Practice Problems 2 This example midterm is extended from Brian Chois file, used in Spring 2011 Problem 1 True False An array index begins with 0. True False If there is a function


  1. CS31: Introduction to Computer Science I Midterm Practice Problems 2 This example midterm is extended from Brian Choi’s file, used in Spring 2011 Problem 1 True False An array index begins with 0. 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 Consider the array is declared as: int a[10]; , there are 10 elements in the array. True False In the C-string (a.k.a char array), we use ‘\n’ to represent the end of a string. True False Function parameters are pass-by-reference by default (e.g., void foo(int n) ) True False We can put a floating number as an index of an array (e.g., a[2.3] ) True False If we declare a C-string (a.k.a char array) of 10 elements, that means this string can hold up to 10 letters (i.e., char arr[10] = “1234567890”; ) Problem 2 True False Which will func1() print? void func1() { if (0 == ‘\0’) cout << “True” << endl; else cout << “False” << endl; } True False Which will func2() print? void func2() { if (0 == true) cout << “True” << endl; else cout << “False” << endl; } 1

  2. True False Which will func3() print? void func3() { if (0 == null) cout << “True” << endl; else cout << “False” << endl; } Problem 3 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. 1 int k[10] = {2, -1, 2, 7, 3, -2, -1, 1, -7, -3}; 2 int steps = 0; 3 int j; 4 cin >> j; // user enters a number. 5 while (j >= 0 && j < 10) 6 { 7 j += k[j]; 8 steps++; 9 } 10 cout << steps << endl; Broadly speaking, there are three outcomes of the program: • Case 1: The program doesn’t compile. (Please describe which line cause the compilation error.) • Case 2: The program compiles, and it prints the result. (Please show what is printed.) • Case 3: The program runs into an invalid state or in a state that not able to print the result. (Please provide the explanation what cause the error.) Your customer is going to supply different numbers to your program. What would be the outcome? The user enters 9. Case _______. Reason __________________________________________________. The user enters 2. Case _______. Reason __________________________________________________. 2

  3. Problem 4 Predict the output of the following code. void mystery(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; mystery(b, c, a); cout << a << " " << b << " " << c << endl; } Problem 5 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. string rotate(string a, int k) { 3

  4. Problem 6 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 a character array) that only consists of lowercase letters and returns true has the “Hebrew” nature, false otherwise. bool isHebrew(char str[]) { } b) Write the function cntHebrews() that takes in an array of words (2D-character array) and counts number of Hebrew words in the array. For example: char words[4][20] = {“xyz”, “ucla”, “sky”, “this”}; int newSize = cntHebrews(words); and newSize should be 2 because “xyz” and “sky” are Hebrew words. The interface of the function is given. Let n be the size of the array. Return -1 if n is negative. Hint: You can use isHebrew() function. int cntHebrews(char words[][20], int n) { } 4

  5. Problem 7 Write a function which initialize a two-dimensional array. The array is a square matrix (i.e., its width and height are identical.) The array should be initialized in the zig-zag style. Specifically, we start at the top-left corner and go downward, and put a number starting from 1. Once we hit the bottom, we go to the next column and fill in the numbers from bottom to top. We fill the numbers downward in the third column, upward in the fourth column, and so on. The process ends when all the elements in the array are filled. At this point, we haven’t decided the array size yet, but we put the array size into a constant variable so that we can change it later. The following are two examples that how you should initialize the array when SIZE is set as different values: SIZE = 5 SIZE = 6 1 10 11 20 21 1 12 13 24 25 36 2 9 12 19 22 2 11 14 23 26 35 3 8 13 18 23 3 10 15 22 27 34 4 7 14 17 24 4 9 16 21 28 33 5 6 15 16 25 5 8 17 20 29 32 6 7 18 19 30 31 The program is partially completed. In fact, besides the main() function, you are given printGrid() for debugging. What you have to do is to complete the initGrid() function. (Turn to next page for the code) 5

  6. #include <iomanip> #include <iostream> using namespace std; const int SIZE = 5; // Note SIZE can be anything between 1 to 9 void initGrid(int grid[SIZE][SIZE]); void printGrid(int grid[SIZE][SIZE]); int main() { int grid[SIZE][SIZE]; initGrid(grid); printGrid(grid); } void initGrid(int grid[SIZE][SIZE]) { // TODO: finish the implementation } void printGrid(int grid[SIZE][SIZE]) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { // setw() function handles the printing format. cout << setw(2) << grid[i][j] << " "; } cout << endl; } } 6

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend