Skip to main content

Section 1.1 Homework 1

The goal of this assignment is to give you some practice writing simple C++ programs and to check your knowledge of some of the main concepts of basic C++.

Exercises Exercises

1.

Write a C++ program that will use ’cout’ and the output stream operator ’<<’ to produce the following output:
Howdy, partner!
What's new in town?
Note: You have been given the first two lines of the program (the #include and using namespace).

2.

Write a C++ program that will output the names of Dr. Wilson’s and Dr. Wahl’s cats, one name per line. Output the names in alphabetical order.
Dr. Wilson’s cats: Ziggy, Cleo, Spike, Skye
Dr. Wahl’s cats: Mr. Grey, Xerea
Note: For this problem you will need to type in the ’include’ and ’namespace’ lines at the top of your program.

3.

The following line of C++ code tells the compiler what?
#include <iostream>
  • Take the code from the file called iostream and paste it into the program before creating the executable.
  • Execute the code in the file called iostream before creating the executable for the current program.

5.

The following line of C++ code tells the compiler what?
int main()
  • There is a function named main that returns an integer.
  • There is a variable named main that is of type integer.

6.

7.

What is the one function that all C++ programs must contain?
  • start()
  • system()
  • main()
  • include()

8.

What header file do you need to use to get access to ’cout’?
  • stream
  • nothing, because cout is available by default
  • iostream
  • namespace

9.

The following line of C++ code tells the compiler what?
int myvar;
  • There is a function named myvar that returns an integer.
  • There is a variable named myvar that is of type integer.

10.

Which of the following is a complete C++ program that will compile and run without error?
  • int main()
    {
        return 0;
    }
    
  • def main():
       return
    
  • main()
    {
       # your code here
    }
    
  • def main()
    {
       return;
    }
    

11.

Which of the following is NOT a valid variable declaration?
  • int a, b, c, d;
  • double pi = 3.14159;
  • cin >> thisisanumber;
  • char letter;

12.

13.

Write a C++ program that reads in two integers entered by the user, adds them together, and outputs the sum.

14.

Write a C++ program that reads in two numbers with a decimal point entered by the user, subtracts the second number from the first, and outputs the difference.

15.

Write a simple calculator program in C++. Your program should:
  • read in two decimal numbers (i.e., numbers may not be integers)
  • give the results for the following calculations
    • adding the two numbers together
    • subtracting the second number from the first
    • multiplying the two numbers together
    • dividing the first number by the second
  • produce output formatted like the given example

16.

Which of the following is NOT one of the basic (i.e., "primitive") data types available to you in C++?
  • string
  • int
  • char
  • double

17.

Given that variable ’num’ currently has the value 0, which of the following segments of code, when executed, will NOT result in ’num’ having the value 3?
  • num == 3;
    
  • num++;
    num++;
    num++;
    
  • num += 3
    
  • num = num + 3;
    

18.

19.

20.

Write a simple C++ program that will read in from standard input three strings representing a person’s last name, first name, and middle initial, and then output the person’s full name formatted like the given example.

21.

Which of the following snippets of C++ code will read a single integer from standard input?
  • int myint;
    cin >> myint;
    
  • double a;
    cin >> a;
    
  • int thisisanumber;
    cout << thisisanumber;
    
  • int myint;
    cin << myint;
    

22.

Which of the following will NOT cause a compiler error (though it *might* cause a compiler warning)?
  • Using a variable without first initializing it.
  • Using a variable without first declaring it.
  • Declaring a variable more than once in the same block of code.

23.

The following line of code will do what?
x -= 5;
  • subtract 5 from the current value of ’x’
  • assign the value 5 to ’x’
  • add 5 to the current value of ’x’
  • compare the value of `x` to 5

24.

25.

Which of the following is NOT a part of the C++ build process?
  • Postprocessing
  • Preprocessing
  • Compiling
  • Linking

26.

When would you get an error related to an undefined function?
  • During the link phase
  • During the compilation phase
  • At program startup
  • When your executable calls the function for the first time

27.

What advantage is there to having separate compile and link steps in the build process?
  • It allows only changed files to be recompiled, saving compilation time.
  • None, it’s confusing and it probably makes things slower since you have multiple programs running together.
  • It makes it easier to diagnose errors because you know whether the problem is from the linker or the compiler.
You have attempted of activities on this page.