Skip to main content

Section 1.2 Homework 2

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 fromΒ chapters 4 & 5:Β  if statements and loops.
Don’t forget to only submit once you have all questions right.

Exercises Exercises

1.

2.

3.

4.

Write a simple calculator program in C++. Your program should:
  • read in the operator symbol (+, -, *, /) as a char
  • read in two decimal numbers (type double)
  • output the result of performing the requested operation on the given numbers
  • format the output as in the given example

5.

Write a C++ program that reads in two integers, and then outputs whether the first number is greater than the second, the first number is less than the second, or the two numbers are equal. See the examples for the correct format of the output.

6.

In C++, which operator is used to compare two values for equality (for example, checking whether a variable has the value 2)?Β  Hint: There is one right answer.
  • ==
  • equal
  • =
  • .equals
  • $

7.

Which of the following expressions evaluate to true? Note: choose all the correct answers.
  • 0
  • true
  • 1
  • (10 <= 3)
  • ! (10 <= 3)
  • false

8.

Which of the following expressions evaluate to TRUE?
  • true && false
  • ! ( false || (false && true) )
  • true || (false && ! true)
  • (true && false) || ! (true || false)
  • false || ! false
  • false && ! false

9.

Which values of x and y will make the following expression evaluate to TRUE?Β  Choose ALL the correct answers.
x != 0 && x * y < 10
  • x = -1, y = 10
  • x = -3, y = -5
  • x = -1, y = 0
  • x = 4, y = 3
  • x = -10, y = -1
  • x = 0, y = 0

10.

Which values of x and y will make the following expression evaluate to FALSE?Β  Choose ALL the correct answers.
x >= 5 || y < 2
  • x = -1, y = 0
  • x = -1, y = 2
  • x = 4, y = 10
  • x = 5, y = 0
  • x = 5, y = 2
  • x = 8, y = 10

11.

Write a C++ program that reads in one decimal number, and then outputs whether the number is positive, negative, or zero. See the examples for the expected format of the output.

12.

Write a C++ program that reads in two strings and compares them. Format your output to correspond with the given examples.

13.

Which part is missing from the for loop below?
for (int n = 0; ; n += 2)
  • Variable initialization
  • Loop condition
  • Variable update

14.

Which part is missing from the for loop below?
for (int n = 100; n >= 0 ; )
  • Variable initialization
  • Loop condition
  • Variable update

15.

16.

Which of the following is NOT a C++ loop structure?
  • for
  • do-while
  • while
  • repeat

17.

Write a C++ program that gets a positive integer from standard input, then outputs the square of all integers from the entered number down to one.Β  See the example for the correct format of the output.
Note: Use a ’for’ loop to solve this problem.

18.

Write a C++ program that repeatedly reads a string from standard input and compares it with the string "my_password".
- If the strings match, the program outputs the message, "CORRECT!", and stops.
- If the strings do not match, and the user has not already made ten attempts, the program reads another string and compares with "my_password" as before.
- If the user’s tenth attempt fails to match "my_password", the program outputs the message, "WRONG!", and stops.
Note: Use a ’for’ loop to solve this problem.

19.

Write a C++ program that reads integers from standard input and keeps a running sum of all the positive integers that are entered. As soon as a negative integer is entered, the program stops and outputs the sum of the positive integers that were read. If the first number is negative, then the result should be 0. Hint: Solve this problem with a whileloop.
Format your output as in the given examples.

20.

Write a C++ program that reads integers from standard input until a number greater than 1000 is entered.Β  At that point, the program stops and prints a report regarding how many numbers were entered that were not greater than 1000.Β  Match the form of your output to the given examples.
Hint: Solve this problem with a whileloop.

21.

In Linux, what is the command pwd used for?
  • print name of current/working directory
  • change password
  • path wrapper disable
  • pluggable window module

22.

In Linux, what is the command cd used for?
  • change directory
  • change distribution
  • check daemon
  • check disk capacity

23.

In Linux, running the cd command (without any arguments) will ...
  • take you to your home directory
  • cache your current directory path
  • check current document for spelling errors
  • list all the files in your current directory

24.

25.

In Linux (and in other systems), a pathgives theΒ unique location of a file or a folder in a file system..
Alternatively referred to as the full path, the ___________ path for a file specifies the root directory and all other subdirectories in which the file is contained.
  • absolute
  • relative
  • proper
  • normal

26.

In Linux (and in other systems), a pathgives theΒ unique location of a file or a folder in a file system..
The relative path for a file specifies the file’s location relative to ...
  • the current working directory
  • the root of the file system
  • the parentof the current working directory
  • the user’s home directory
You have attempted of activities on this page.