Question

In: Computer Science

5C++ Questions 1. What will the following code print? num = 8; cout << --num <<...

5C++ Questions

1. What will the following code print?
num = 8;
cout << --num << " ";
cout << num++ << " ";
cout << num;

2.Since the while loop evaluates the condition before executing the statement(s), it is known as a(n)____ loop, whereas the do-while loop evaluates the condition after the statements have been executed, it is known as a(n)____ loop.

3.T/F While loops can only be used when a counter is decremented to zero. If the counter is incremented you must use the do-while loop.

4.How many times will the following program print Hello?

counter = 3; 
while (counter > 0) {
    loop = 5;
    while (loop > 0) {
          cout << "Hello" << endl;
          loop--;
     }
     counter--;
}

5.If nothing within a while loop causes the condition to become false, a(n) ________ may occur.

6.The -- operator ... (check all that apply)

subtracts one from the value of its operand.

must have an lvalue, such as a variable, as its operand.

can be used in either prefix or postfix mode.

is a unary operator.

7.Which of the following statements will result in an infinite loop.

x = 10; 
while (x >= 0) {
    cout << x;
    ++x; 
}
x = 10; 
while (x >= 0) {
    cout << x;
}
x = 0; 
while (x <= 10) {
    cout << x;
    x = 10;
}
x = 10; 
while (x >= 0); {
    cout << x;
    x--;
}

8.T/F In C++, a file must be opened before the contents can be read.

9. T/F In C++, a file must exists before it can be written to.

10.Which of the following statements show the proper way to use the increment (++) operator.

x++ = 1023;

x = 23++;

y = ++x;

counter--;

Solutions

Expert Solution

Question 1:
Output of the code 7 7 8
Answer:
7 7 8

Question 2:
Since the while loop evaluates the condition before executing the statement(s), it is known as a(n) pre condition loop, whereas the do-while loop evaluates the condition after the statements have been executed, it is known as a(n) pre condition loop.
Answer:
pre condition
post condition

Question 3:
While loops can only be used when a counter is decremented to zero. If the counter is incremented you must use the do-while loop.
Answer:
False

Question 4:
Number of times will the following program print Hello is 15
Answer:
15

Question 5:
If nothing within a while loop causes the condition to become false, a(n) infinite loop may occur.
Answer:
infinite loop

Question 6:
The -- operator can be used in either prefix or postfix mode.
Answer:
can be used in either prefix or postfix mode.

Question 7:
x = 10;
while (x >= 0) {
    cout << x;
    ++x;
}
x = 10;
while (x >= 0) {
    cout << x;
}
x = 0;
while (x <= 10) {
    cout << x;
    x = 10;
}
will result in an infinite loop
Answer:
Options 1,2,3

Question 8:
In C++, a file must be opened before the contents can be read.
Answer:
True

Question 9:
In C++, a file must exists before it can be written to.
Answer:
False

Question 10:
statements show the proper way to use the increment (++) operator is y = ++x;
Answer:
y = ++x;


Related Solutions

What will the following code segments print on the screen? int num = 3; switch (num){...
What will the following code segments print on the screen? int num = 3; switch (num){             case 1:                         System.out.println(“Spring”); case 2:                         System.out.println(“Summer”); case 3:                         System.out.println(“Autumn”); case 4:                         System.out.println(“Winter”); }
Viewing the code snippet below answer the following questions: int num=10; do {      cout << “Hello”...
Viewing the code snippet below answer the following questions: int num=10; do {      cout << “Hello” << endl;      num--; } while(num > 1); After the above code is executed: “Hello” printed how many times: ___________ What is value ofnum:  ____________ language is c++
(True or False) The following function will compile. void print(int num) { return num+1; } Group...
(True or False) The following function will compile. void print(int num) { return num+1; } Group of answer choices True False Flag this Question Question 2 10 pts (True or False) The following code will output "I was true". bool isGreater(string s1, string s2) { if(s1 > s2) { return true; } return false; } int main() { if(isGreater("before","back")) { cout << "I was true"; } else { cout << "I was false"; } return 0; } Group of answer...
(PYTHON) prompt the user to enter a single-digit num and print that num as follow: 1)...
(PYTHON) prompt the user to enter a single-digit num and print that num as follow: 1) use loops 2) print num in words. Example: 5 is 'five' 3) the number of spaces that go back and forth should equal the num if the user inputs 5, the program prints: five . . . . .five five . . . . . five five and 2 would be: two . .two (the periods(.) are only for demonstration purposes. Don't include it...
Q1: Given the following code, what is returned by tq(4)? int tq(int num){ if (num ==...
Q1: Given the following code, what is returned by tq(4)? int tq(int num){ if (num == 0) return 0; else if (num > 100) return -1; else     return num + tq( num – 1 ); } Group of answer choices: 0 4 -1 10 Q2: Given that values is of type LLNode<Integer> and references a linked list (non-empty) of Integer objects, what does the following code do if invoked as mystery(values)? int mystery(LLNode<Integer> list) {    if (list.getLink() ==...
What is the ouput of the following code? void loop(int num) { for(int i = 1;...
What is the ouput of the following code? void loop(int num) { for(int i = 1; i < num; ++i) { for(int j = 0; j < 5; ++j) { cout << j; } } } int main() { loop(3); return 0; }
Why does my code print nothing in cout? I think my class functions are incorrect but...
Why does my code print nothing in cout? I think my class functions are incorrect but I don't see why. bigint.h: #include <string> #include <vector> class BigInt { private:    int m_Input, m_Temp;    std::string m_String = "";    std::vector<int> m_BigInt; public:    BigInt(std::string s)   // convert string to BigInt    {        m_Input = std::stoi(s);        while (m_Input != 0)        {            m_Temp = m_Input % 10;            m_BigInt.push_back(m_Temp);       ...
use cout to print the address at which the following array is stored in memory long...
use cout to print the address at which the following array is stored in memory long double computers[24] a. print the adress of the last element b. print the address of the tenth element. c. print the address of the first element in the array
What output is produced by the following code fragment? int num = 0, max = 20;...
What output is produced by the following code fragment? int num = 0, max = 20; while (num < max) { System.out.println(num); num += 4; }
Question 1: What does the following code print to the console when the main method is...
Question 1: What does the following code print to the console when the main method is run? public static void referenceMystery(int x, int[] a) { x = 3; a[0] = 4; System.out.println(x+" "+a[0]); } public static void main(String[] args) { int x = 1, y = 2; int[] a = new int[1]; int[] b = new int[1]; referenceMystery(y, b); } 1. x a[0] 2. 1 3. 2 0 4. 3 4 Question 2: What does the following code print out...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT