e01 : Midterm exam - I
num | ready? | description | exam date |
---|---|---|---|
e01 | true | Midterm exam - I | Thu 04/25 05:00PM |
Exam logistics
Thursday, 4/25 in the regular lecture classroom at the usual lecture time
- Exam begins exactly at the start of the lecture - Please start arriving 5-10 minutes before class
- When seated, try to leave two empty chairs between yourself and your neighbor(s).
You may bring only one 8.5” x 11” piece of paper with notes on both sides.
- Make sure that your name is on both sides of the paper in the top right corner.
-
Your note sheet has to be hand-written (i.e., not typed).
-
No books, calculators, phones, laptops or other materials or devices are allowed during the exam.
- Make sure to READ all instructions carefully.
Open Review Session and Extra Office hours
The Open Review Session information was posted on Piazza. Feel free to stop by at any time during that interval to ask for help with any of the topics or practice problems.
Just this week, Prof. K will host additional office hours on Wednesday, 4/24, 2PM-6PM in her office in HFH 2107. Feel free to stop by at any time to ask for help with any of the topics.
Have fun practicing what you’ve learned so far!
Remember: programming is not a spectator’s sport – you have to actively write code in order to get better at writing, reading, and debugging it – just looking at the code examples is not enough.
We recommend that you take the example code shown in the lectures and try to write it from scratch, without looking at the solution. Then, compare your code to the provided solution and note any differences – look into and understand whether these differences are due to style or they are important.
Main Exam Topics
- Intro to Computers, Programming, and C++
- Variables and Assignments
- Boolean Expressions (comparison of variables)
- Input and Output (cout, cin)
- Data Types, Escape Sequences, Formatting Decimal
- Arithmetic Operations and their Priorities
- Boolean Logic Operators
- Flow of Control & Conditional Statements
- Loops: for, while, do-while
- Multiway Branching and the switch command
- Functions in C++: pre-defined, user-defined void functions, the main() function
- Types of Errors in Programming
- Command Line Inputs to C++ Programs
- Vim / Unix navigation
Lecture Topics for the Exam Study Guide
- Unix Commands (Lecture 2)
- Relative Vs Absolute Path (Lecture 2)
- Vim Commands (Lecture 2, vim : basic eight)
- Writing, Compiling and Running a C++ Program (Lecture 2)
- C++ Variables: Initializing, Modifying and Assigning (Lecture 3)
- Boolean Expressions (Lecture 3)
- Control Structures (Lecture 3)
- Multi-way If-else Statements (Lecture 3)
- User Input/Output (Lecture 4)
- Coding Process (Lecture 4)
- Initialization vs Declaration (Lecture 4)
- while loop (Lecture 4)
- do-while loop (Lecture 4)
- for loop (Lecture 4)
- Skipping and Terminating Loop Execution (Lecture 4)
- Nested Loop (Lecture 4)
- Formatting Output to Terminal (Lecture 4)
- Example : A Number guessing game (Lecture 4)
- Example : User command line arguments (Lecture 5)
- Functions (Lecture 5)
- Example : A Simple Function Definition
- Return Vs Print
- Memory Stack
- Switch Statements (Lecture 6)
Practice Problems
Note: these are just a few practice problems to give you additional practice and a sample of what the potential exam questions might look like.
Additional Practice Tips and Problems are included in the Lecture 7 slides and notes.
1.
Write the output for the following statements.
A.
int x = 10;
int y = 2;
while(x > 0){
cout << x + y << " ";
x = x - 2;
y = y + 3;
}
B.
for ( int i = 0; i < 5; i++){
for( int j = 0; j < i; j++){
cout << "! ";
}
cout << endl;
}
2.
Rewrite the following function using “switch” and “case” so that it has the same functionality.
void f (int num){
if (num == 10){
cout << "You said 10" << endl;
}
else if (num == 15){
cout << "You said 15" << endl;
}
else {
cout << "Everything else" << endl;
}
}
- Can you optimize the above example to remove repetition in printing “You said …” for different cases?
3.
What is the output for the following statements? If it results in a compilation error, briefly explain why and fix the error.
#include <iostream>
using namespace std;
int main(){
f();
return 0;
}
int f(){
cout << "f()" << endl;
return 0;
}
4.
Write UNIX commands to perform following tasks.
(a) Compile the file main.cpp into a binary executable named main.
(b) Run the executable produced in part (a).
(c) Delete the executable we just created.
5.
Write a function calc_sum
that takes in a parameter integer n
and returns the value 1 + 4 + 9 + … + n^2. If n
is less than 1, the function returns 0.
6.
Write a complete program that takes command line arguments, a, b, c and x. The program will print the result of a*x^2+b*x+c
on the terminal. For example, ./main 1 2 3 4
, will effectively mean that a=1, b=2, c=3, x=4
, and the program will print 27 (1*4^2+2*4+3)
. If the incorrect number of is provided, print a message to tell the user correct usage and exit the program.
7.
Given the following code
int x;
for (x = 10; x>0; x=x-2) {
cout << x << endl;
}
a. What is the output for the given code?
b. Write code that produces the same output but that uses a while
loop instead of a for
loop.
8.
The following code prints out a block of stars (assume all variables have been initialized). What variable determines the total number of rows and which variable determines the total number of columns?
for (int a = 0; a < b; a = a + 1){
for (int c = d; c > 0; c--){
cout << "*";
}
cout << endl;
}
9.
- Write a program that calculates the area of a trapezoid to 3 decimal places. The program receives the length of the 2 bases and the height as command line arguments. The program should accept exactly 3 arguments from the command line. (Topics: Command line arguments, Integer division)
10.
Write the output of the following program:
#include <iostream>
using namespace std;
int foo(int i){
cout << i;
return i*i;
}
int main(){
if(foo(2) == 2)
cout << "Fizz";
else
cout << foo(2);
return 0;
}
(Topics: Return vs print, Functions)
11.
Write UNIX commands for the following tasks (a) Make a directory called myFolder in the current directory
(b) Rename the file main.cpp to hello.cpp. Assume main.cpp is in the current directory.
(c) Delete the directory myFolder.
12.
What is the output of calling foo(1)
? What about foo(5)
?
void foo(int i) {
switch (i) {
case 1:
cout << "foo(1)" << endl;
// no break statement!
case 2:
cout << "foo(2)" << endl;
default:
cout << "Default" << endl;
}
}
13.
Write the Vim commands/keystrokes for the following tasks:
- Save and quit (the 2 ways we covered in class)
- Enter into insert mode (from the command mode)
- Exit out of insert mode back into command mode
- Delete a whole line (from the command mode)
- Copy 2 lines and paste them (from the command mode)
- Quit without saving
- Bonus: Jump to the beginning / end of the line (from the command mode)
- Bonus: Jump to the beginning / end of the file (from the command mode)
14.
What is wrong with the following code?
int x;
If (x == 0){
cout << "hello" ;
}