CSCI 131 - Techniques in Programming, Spring 2014

    Home | | Schedule | | Assignments | | Lectures | | Resources

    Topics for Exam1: This sheet is intended to help you prepare for the first exam in this course. The following topics have been covered in the first 5 weeks of the course. The exam will cover chapters 1 - 6 and 9 of the textbook, and all of the lectures through the "Loops" section of Lecture 12 on Monday, 2/17/14. It will also cover labs 1 - 4 and projects 0, 1 and 2. Each of the following topics may appear on the exam.

    1.  Problem solving techniques
    	Divide, Conquer and Glue
    	Abstraction
    	Black box abstraction
    2.  Basic Syntax
    	Form of C++ program
    	Simple Data types: float, double, int, char
    	Identifiers, Variables and Constants
    	Assignment statements
    	Arithmetic Expressions
    		Precedence rules
    		Associativity
    	Type conversion and Type Casts
    	Calling functions
    	Using C++ libraries
    3.  Input and Output
    	Filestreams, cin and cout
    	insertion and extraction operators
    	output formatting (setf, setprecision(), setw())
    	Interactive I/O
    	get(), ignore() 
    	File input and output
    	opening and closing files
    	Stream fail states.
    4.  Conditional Statements
    	Basic syntax
    	Boolean (true/false) expressions
    	Relational operators
    	Logical operators
    	Short circuiting
    	Nested if
    5.  Switch
    	Syntax
    	Writing cases
    	Using break statements
    6.  Loops
    	while loops
    	for loops
    	do-while loops
    	event-controlled loops
    	count-controlled loops
    	nested loops
    


    Practice Problems

    The following problems are intended to help you study for the exam, however topics not covered here may be on the exam as well. Use your text, class notes, labs and assignments to review for the exam as well.

    1) Consider the following statement:

      players = teamA + teamB;

    a) What kind of C++ statement is this?

     

    b) What is the first thing C++ does when executing this statement?

     

     

    2) Consider the following function declaration:

      float cube(float number);

    a) What does the float at the beginning of the line refer to?

     

      b) Write a statement to find the cube of 7.3 and assign the result to a variable named answer.

     

     

    3) Compute the value of the following expressions:

    a)   30 / 5 % 4 + 2

     

    b)   (17 - 11) / 3 + 3

     

    c) Add parentheses to part b so that the addition is performed before the division.

     

    d) What is the value of the expression in part c ?

     

     

    4) Write a program that does the following:

      1) Prompt the user to input a number.
      2) Input the number from the keyboard
      3) Echo print the number
      4) If the number is less than 100, write:
        XX is a low number.
      where XX refers to the number inputted by the user.
      5) Otherwise, write:
        XX is a high number.

     

     

     

     

     

     

     

     

     

     

    5) Given these boolean variables:

      x = true, y = false, z = true

    What is the value (true/false) of each of the following?

      i)     x && (y || z)

       

      ii)   !x || !(y && z)

       

      iii)   y || (x && !z)

     

    6) What is the output after executing the following switch:

      int Day = 4;
      switch (Day) {
         case 1:
            cout << "Sunday" << endl;
         case 2:
            cout << "Monday" << endl;
         case 3:
           cout << "Tuesday" << endl;
         case 4:
           cout << "Wednesday" << endl;
         case 5:
           cout << "Thursday"<< endl;
         case 6:
            cout << "Friday" << endl;
         case 7:
            cout << "Saturday" << endl;
      }	
      

     

     

     

     

     

    7) Consider the following while loop:

      	sum = 0;
      	count = 5;
      	while (count <= 10) {
      		sum = sum + count;
      		count ++;
      		//show values here
      	}
      

    a) Fill in the following table for the values of count and sum each time through the loop (there may be more rows than you need in the table):

      sumcount
      05

    b) Write a for loop that does the equivalent to the while loop in 7a.

     

     

     

     

    8) Consider the following C++ source code:

      	int a = 4;
      	int b = 3;
      	float c;
      
      	c = a/b;
      

    a) What is the value of c after executing the above 4 lines?

     

    b) Rewrite the 4th line using type-casts to make C++ perform floating point division for the value of a divided by b.

     

     

    9) Consider the following C++ code:

    	float x = 7.3;
    	float y = 8.4;
    	ofstream outFile;
    
    	outFile.open("review.out");
    
    	cout.setf(ios::fixed, ios::floatfield);
    	cout.setf(ios::showpoint);
    
    	outFile << "The value of x*y is: " << x*y;
    

    Why isn't the result displayed in fixed point notation, but instead shown in exponential notation?

     

     

    10) Write a conditional that accomplishes the following:

    i) If gender is 'F' and height is greater than 70 inches, invite the user to tryout for the women's basketball team.

    ii) If gender is 'M' and height is greater than 75 inches, invite the user to tryout for the men's basketball team.

    iii) Otherwise, print out "Basketball is not for you."

    Start with this code:

    	int height;
    	char gender;
    	cout << "Enter your height in inches: ";
    	cin >> height;
    	cout << "Enter your gender (M/F): ";
    	cin >> gender;
    	//write conditional here.
    

     

     

     

     

     

     

     

     

     

     


    Home | | Schedule | | Assignments | | Lectures | | Resources


    Constance Royden--croyden@cs.holycross.edu
    Computer Science 131
    Last Modified: February 23, 2014
    Page Expires: August 10, 2014