midterm practice problems
play

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

CS31: Introduction to Computer Science I Midterm Practice Problems This example midterm is extended from Brian Chois file, used in Spring 2011 Problem 1 True False Variable names can begin with an alphabet or a digit. True False A


  1. CS31: Introduction to Computer Science I Midterm Practice Problems This example midterm is extended from Brian Choi’s file, used in Spring 2011 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 (i.e., char a = ''; ) 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 for (int i = 0; i <= 49; i++) will run the loop 50 times (assuming i is not modified within the loop). 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 main() { int i = 0; int k; cin >> k; if (k < 0) return -1; while (k > 0) { if (k % 3 == 0) i++; k--; } cout << i; return 0; } 1

  2. Problem 3 Your friend at U$C is also taking a CS31-equivalent course at his institution. For his project, he has to write a program that takes in an integer as the input and prints a triangle based on the number. Here are the example runs of the function: Input is 3 Input is 1 Input is 5 * * * *** *** ***** ***** ******* ********* Note: 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: 1 int main() { 2 int size; 3 cout << "Please enter the triangle size: "; 4 cin >> size; 5 6 // error if size is not a positive number 7 if (size > 0) { 8 cout >> "Not a valid size!!” << endl; 9 return 0; 10 } 11 12 int i = 0; 13 14 while (i < size) { 15 // Add spaces in front. 16 for (int j = 0; j < size - i; j++) { 17 cout << " "; 18 } 19 20 // Draw the stars. 21 for (int j = 0; j < i; j++) { 22 cout << "*"; 23 } 24 25 cout << endl; 26 i++; 27 } 28 29 return 0; 30 } 2

  3. 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. Problem 4 The following criteria can help us to decide which year is a leap year: • If the year number is divisible by 4, it is a leap year. • However, if it is divisible by 100, it is not a leap year. • However, if it is divisible by 400, it is again a leap year. Based on these criteria, can you write a program to determine whether the given year is a leap year? Please print the result in the following format: 1994 is not a leap year. or 2000 is a leap year. int main() { int year; cout << "Enter the year: "; cin >> year; // Your code here to determine the leap year return 0; } 3

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