ENJOY! 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;

Similar documents
Discussion 1H Notes (Week 2, 4/8) TA: Brian Choi Section Webpage:

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Discussion 1H Notes (Week 4, April 22) TA: Brian Choi Section Webpage:

Midterm Practice TA: Brian Choi Section Webpage:

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

CS31: Introduction to Computer Science I Spring 2011

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 1

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Discussion 2C Notes (Week 5, February 4) TA: Brian Choi Section Webpage:

CS 240 Data Structure Spring 2018 Exam I 03/01/2018

Midterm Exam. Sample Solutions

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

CSE413 Midterm. Question Max Points Total 100

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

CS31 Discussion 1E. Jie(Jay) Wang Week1 Sept. 30

Review. Relational Operators. The if Statement. CS 151 Review #4

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

Discussion 2C Notes (Week 5, February 4) TA: Brian Choi Section Webpage:

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

CS Semester I. Quiz 1 (version A)

CS31 Discussion 1E. Jie(Jay) Wang Week5 Oct.27

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

CSCE 121 ENGR 112 List of Topics for Exam 1

CSC 126 FINAL EXAMINATION FINAL Spring 2012 B. Name (last, First) Instructor. Total Possible. Received

CSCI 111 First Midterm Exam Fall Solutions 09.00am 09.50am, Wednesday, October 18, 2017

The second statement selects character number 1 from and assigns it to.

CSE143 Exam with answers MIDTERM #1, 1/26/2001 Problem numbering may differ from the test as given.

Introduction to Programming I COS1511 School of Computing Revision Notes

Reviewing all Topics this term

The C++ Language. Arizona State University 1

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

Programming Language. Functions. Eng. Anis Nazer First Semester

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

REPETITION CONTROL STRUCTURE LOGO

EECS 183, Week 5. General. Variables I/O. 0. At which location do you have to take the exam? 1. Source code vs. object code? 2. What s a library?

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 03

Midterm Practice Exam

CSCI 102 Fall 2010 Exam #1

Getting started with C++ (Part 2)

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

CS31 Discussion 1E Spring 17 : week 08

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

CS31 Discussion. Jie(Jay) Wang Week6 Nov.4

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

CS 106B Practice Midterm Exam #1 (by Marty Stepp)

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Problem 1: Get val soon! (15 marks)

Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution

CSE 333 Midterm Exam July 24, Name UW ID#

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

CS 2150 Final Exam, Spring 2018 Page 1 of 10 UVa userid:

CS242 COMPUTER PROGRAMMING

The following expression causes a divide by zero error:

Topic 2: Making Decisions

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

EECS 183. Week 3 - Diana Gage. www-personal.umich.edu/ ~drgage

Topic 2: Making Decisions

Practice test for midterm 2

CSCE Practice Midterm. Data Types

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

C++ Support Classes (Data and Variables)

CS 201 (Intro. to Computing) Fall 2015 Sabancı University Sample Questions for Midterm 1

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

CS2141 Software Development using C/C++ C++ Basics

Week 2. Muhao Chen.

C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved. Using a Debugger WE5.

CS32 Discussion Sec.on 1B Week 2. TA: Zhou Ren

To become familiar with array manipulation, searching, and sorting.

VARIABLES & ASSIGNMENTS

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Computer Programming : C++

CMSC 202 Midterm Exam 1 Fall 2015

CS 2150 Exam 1, Spring 2018 Page 1 of 6 UVa userid:

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Why Is Repetition Needed?

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

If Control Construct

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

Week4. Huayi Li

conditional statements

Al Imam Mohammad Ibn Saud Islamic University College of Computer and Information Sciences Computer Science Department

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

CS 104 (Spring 2014) Final Exam 05/09/2014

CSCI 111 First Midterm Exam Spring Solutions 09.05am 09.55am, Wednesday, March 14, 2018

1. In C++, reserved words are the same as predefined identifiers. a. True

CSE 142 Sample Midterm Exam #3

CSE wi: Practice Midterm

ComS 228 Exam 1. September 27, 2004

Transcription:

Midterm Practice Problems TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 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

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. 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. 3. A runtime error occurs. Now suppose user enters 2. 1. It successfully compiles and prints something. Q: What does it print? 2. A compile error occurs. 3. A runtime error occurs. Copyright Brian Choi 2011. All Rights Reserved. Week 5, Page 2/5

Problem 4 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

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 eabcd rotate( abcde, 2) returns deabc rotate( abcde, 3) returns cdeab rotate( abcde, 7) returns deabc Do not rotate the string if k is negative. Copyright Brian Choi 2011. All Rights Reserved. Week 5, Page 4/5

Problem 7 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