CSCI 102 - Fall 2023 Fundamentals of Computation

Jump to answers

Questions

Q1:

What is the value of c after the following lines execute?

   int a = 5;
   int c = a;
   a -= 3;
  1. c = 5
  2. c = 3
  3. c = 2
  4. c = -1

Q2:

What is the value of c after the following lines execute?

   int a = 5;
   int b = a;
   b + 1;
   int c = b;
  1. c = 1
  2. c = 5
  3. c = 6

Q3:

What is the value of b after the following lines execute?

   int a = -2;
   int b = a; 
   int c = b + 7; 
   a++; 
   b + =a; 
   b * =c; 
   b = -b % c; 
  1. b = 5
  2. b = -5
  3. b = 0
  4. b = 3

Q4:

What are the values of variables a, b, and c at the end of the program?

#include <iostream>
using namespace std;
int main()
{
	int a = 5;
	int b = a;
	int c = 3;

	a = 25 % c;
	b = b * (1 / b);
	c++;
	return 0;
}

Q5:

What is output to the screen by this program?

#include <iostream>
using namespace std;
int main()
{
	int a, b,c;
    a=5;
    b=a + 2 * a;
    c=b*a+b/a;

    cout << c << endl;
	c%=a
	cout << c << endl;

	return 0;
}

Q6:

Which of these will compile?

  1. int x; cin >> x;
  2. int x = 0; cin >> x;
  3. int x; cin << x;
  4. cin >> x; int x;

Q7:

The user types the following text as input to the program:

97 is 'a'

What are the values of x, y, and z at the end of the program?

#include <iostream>
using namespace std;

int main() {
    char c;
    string s1;
    string s2;

    cin >> c >> s1 >> s2;

    cout << "c: " << c << endl;
    cout << "s1: " << s1 << endl;
    cout << "s2: " << s2 << endl;

    return 0;
}

Q8:

Which line of code will output 12? Assume <iomanip> is included.

  1. cout << 1 2 << endl;
  2. cout << setprecision(0) << fixed << 12.5 << endl;
  3. cout << setw(2) << 1 << setw(2) << 2 << endl;

Q9:

What will the following code output?

#include <iostream>
using namespace std;

int main()
{
    double a = 0.5;
    char b = ',';
    bool c = false;
    string d = "Hello";

    for(int i = 0; i < 3; i++){
        cout << a + i << b << ' ';
    }
    cout << a+3 << endl;
    cout << boolalpha << c << 'd' << endl;
}

Q10:

What will be stored in x after the following expression is evaluated:

double x = ((int)4.75 % 3) % 5; 
  1. The code will not compile because modulo may only be used with integers
  2. The code will not compile because you cannot assign the result of a modulo operation to a double
  3. x = 2.0
  4. x = 1.0
  5. x = 0.0

Q11:

What will be stored in the variable y after the following line of code is executed?

int y = 3.0 / (2 % 100) * (25.0 + 8) + 8;
  1. 56
  2. 56.5
  3. 57
  4. 57.5
  5. You will get an error message.

Q12:

What will be stored in a,b, and c at the end of this code snippet:

int a = 11;
int b = 3;
int c = 10;
double d = 3.99;

a = (a + b) / (double)b;
b = c / (int)d;
c = (double)(b * 1.0) / 4.0;

Q13:

What will be printed to the terminal in the following program?

double a = 3.5;
double b = 10.0;
int x = 4;
int y = b/x;


cout << a + b << endl;
cout << b / x << endl;
cout << y << endl;
cout << a * b * y << endl;

Q14:

What will the following code output?

int a = 0;
int b = 2;
double c = 4.0;
double d = 4.0;

a++;
b += 3;
c = a / (int)d + a / c;
d = (a * 1.0) / (int)d + a / b;

cout << a << " " << b << " " << c << " " << d << endl;

Q15:

What will be printed as a result of this code?

int w = 5;
int x = 3;
double y = 4.0;
double z = 2.5;

int a = x + y / z - w;
int b = (double) w / 2 + 3.5;
y = 5.0 * b - a + 0.25;

cout << a << " " << b << " " << y << endl;

Q16:

True or False: You should avoid calling cin and cout within the body of a for loop, as it may upset the loop counter behavior.

Q17:

True or False: ‘for’ loops check the condition after axecuting the body of the loop at least once and then repeat the body if the condition is true.

Q18:

True or False: The loop below is an infinite loop (will run forever)?

for(int i = 1; i < 10; i += 2){
    cout << i << endl;
}

Q19:

What will be the output for the following program?

#include <iostream>
using namespace std;

int main()
{
        int a = 2;
        int b = -1;
        for (b = 5; b > 0; b--){
            a *= b;
        }

        cout << a;
        return 0;
}

Q20:

Which of the following are necessary for a function prototype? (Select all that apply.)

  1. Number and type of argument/parameters
  2. Value to be returned
  3. Type to be returned
  4. Function name

Q21:

What is the output of the code below?

int f1 (int x, double y)
{
    if (x/(int)y > 0)
        return x/y;
    else
        cout << x * y << endl;
        return x * y;
}

int main()
{
    int x = 5;
    double y = 2.0;
    x = f1(x, y);
    y = (double)f1(x, 3.5);
    cout << (y-2)/2 << '\n';
    return 0;
}

Q22:

Based on the structure of the code and allowing any possible value of the variable a, what are all the actions that could execute?

if (a < 5) 
{		
    if(a >= 2)
    {
        // action 1
    }
    else if(a == 2)
    {
        // action 2
    }
    // action 3
}
else if(a < 10)
{
    if(a <= 5)
    {
        // action 4
    }
    // action 5
}
  1. 1, 3, 4, 5
  2. 1, 2, 5
  3. 1, 2, 3, 4
  4. 1, 3, 5
  5. None of the above

Q23:

Which of the following is true?

  1. We can have more than one else blocks
  2. We can have only one else if block
  3. In an if condition, the compiler will interpret any non-zero result of the condition to mean true
  4. if blocks must be executed at least once

Q24:

Which blocks are executed in the code below?

bool skyBlue = true;
bool threeWheat = true;
bool bread = true;
char gameMode = 'C';

if(threeWheat == bread){
	// block 1
}
else if(gameMode == 'C'){
	// block 2
}
else if(threeWheat == bread){
	// block 3
}
else{
	// block 4
}

if(skyBlue == bread){
	// block 5
}
if(threeWheat == skyBlue){
	// block 6
}
else if(gameMode != 'B'){
	// block 7
}
else{
	// block 8
}
  1. 1,2,3,5,6
  2. 1,5
  3. 1,5,6
  4. 1,5,6,7

Q25:

What will the following code output?

int a = 0;
int b = 1;
int c = 2;

b = c * c;
if(a < b && b == 4)
{
    c = 10;
    if(c - a > b + 4)
    {
        c = a;
    }
    else
    {
        c = b + a;
    }
    b = a - b + c;
}
else
{
    b += 9 + c;
}
a = c + 3;
if(a < b || b > 1)
{
    a += b + c;
}
cout << "a = " << a << "; b = " << b << "; c = " << c << ";" << endl;
  1. a = 7; b = 0; c = 4;
  2. a = 3; b = -4; c = 0;
  3. a = -1; b = -4; c = 0;
  4. a = 7; b = 4; c = 0;

Q26:

Each student has an ID. The ID number can be decoded to get some basic information of the student, including legacy, year in school, and major. Assume the user inputs an id of 140557.

What will the program print for that input?

int id; 
cout << "Please enter the student ID: ";
cin >> id;

if(id % 10 % 2) {
    cout << "Legacy" << endl;    
}
else {
    cout << "Non-legacy" << endl;    
}

int a = id / 1000;
int b = id % 1000;

if(a < 300 || b > 200) {
    cout << "Viterbi School of Engineering" << endl;
    
    if(a + b < 500) {
        cout << "CSCI Major" << endl;  
    }
    else if(a - b < 0) {
        cout << "CSBA Major" << endl;    
    }
    else {
        cout << "CECS Major" << endl;    
    }
}
else {
    cout << "Marshall School of Business" << endl;
    
    if(a + b > 500) {
        cout << "BUAD Major" << endl;  
    }
    
}

id /= 100;
int c = id % 10;

if(!(c % 3)) {
    cout << "Freshmen" << endl;    
}
else if (c % 5 == 0) {
    cout << "Sophomore" << endl;    
}
else if (c / 4 >= 3) {
    cout << "Junior" << endl;    
}
else {
    cout << "Senior" << endl;    
}

Q27:

Understand the code below and then answer the following questions:

bool feelingSick = 1;
bool goneToKeck = false;
int wonderingIfCovid = 1;
bool goToSchool = false;
if(feelingSick){
	wonderingIfCovid++;
	if(!goToSchool){
		if(goneToKeck){
			cout << "waiting for test results" << endl;
		}
		else if(!goneToKeck){
			wonderingIfCovid += 2;
			cout << "get tests done" << endl;
			goneToKeck = true;
			if(!feelingSick){
				goToSchool = true;
			}
			else if(wonderingIfCovid == 2){
				cout << "it's probably something else" << endl;
				goToSchool = false;
			}
			else{
				cout << "test results are completed and consequently lost" << endl;
				if(feelingSick && wonderingIfCovid >= 5){
					cout << "I'm in danger" << endl;
				}
				else if(feelingSick && wonderingIfCovid <= 2){
					goToSchool = false;
				}
				else{
					cout << "The director of Keck calls and apologizes" << endl;
				}
			}

		}
		else{
			cout << "The director of Keck calls and apologizes" << endl;
		}

		goneToKeck = true;
		wonderingIfCovid -= 3;
	}
}
else{
	wonderingIfCovid--;
	goToSchool = true;
	if(goToSchool){
		cout << "TA Megan's puns in lab encouraged me to attend" << endl; 
	}
}

Q28:

Which of the following returns false if x is set to 10?

  1. x >= 0
  2. x != 5
  3. x > 10
  4. x <= 100

Q29:

What does the following program output?

#include <iostream>
using namespace std;
int main()
{
	int code = 25;
	if((code / 2) > 12)
	{
		cout << "A" << endl;
	}
	else if((code == 25) && (code < 25))
	{
		cout << "B" << endl;
	}
	else if(!(code > 10) || (code != 50))
	{ 
		cout << "C" << endl;
	}
	else
	{
		cout << "I have no idea" << endl;
	}

	return 0;
}

Q30:

What does the following program output?

#include <iostream>
using namespace std;
int main()
{
   bool A = 1 > 1;
   bool B = 0 <= 3;
   if (A || B) {
       cout << "1" << endl;
   }
   if (A && B) {
       cout << "2" << endl;
   }
   if (!A && B) {
       cout << "3" << endl;
   }
   return 0;
}

Q31:

True/False: The code below will run forever.

while (1) {
    cout << "hello 102";
    int a = 0;
    if (a != 0) break;
    ++a;
}

Q32:

Is this valid syntax for a while loop condition?

	int x = 0;
	int y = 1;
	while (max(x,y)) 
	{
		cout << "I love CS102!" << endl;
	}
  1. Yes, the compiler will interpret the result of the function as a Boolean.
  2. Yes, the compiler interprets the condition as false and never enters the loop.
  3. No, the compiler will not be able to interpret the function as a Boolean.
  4. No, max(x,y) is not a valid function and will not compile.

Q33:

How many times will Here be printed by this loop execute?

int i = 0;
while(i < 10)
{
  cout << "Here" << endl;
  if(i % 2 == 0)
  {
     i++;
  }
  if(i == 5)
  {
    break;
  }
  i++;
}

  1. 3
  2. 10
  3. 5
  4. the loop is infinite

Q34:

Given an example of inputs that will produce the output n = 8

#include <iostream>
using namespace std;
int main()
{
    int i=0, j, x, y, n = 0;
    cout << "Input two integers:" << endl;
    cin >> x >> y;
    if (y < 11)
    {
      while(i<=x)
      {
          for (j = 0; j<=y; j++)
        {
          if(i >= 5 || j >= 10)
          {
            break;
          }
          if(i%2 == 0 && j%3 == 0)
          {
              n+=1;
          }
        }
        i++;
      }
    }
    cout << "n = "<< n <<endl;
    return 0; 
}

Q35:

What will be the value of x after the following code is executed?

int x = 0;
int n = 3;
while (n > 1)
{
    if (n % 2 == 0)
    {
        n = n / 2;
    }
    else
    {
        n = 3 * n + 1;
    }
    x++;
}

Q36:

What will be printed by this snippet of code?

int a = 0, b = 1;
while (a || b) {
    a *= b;
    b *= a;
    cout << a << ", " << b << endl;
}

Q37:

What initial values for num_1 and num_2 would cause Congratulations to be printed on the last line of the following code snippet?

int num_1 = ??;
int num_2 = ??;
int a = 100;
int count = 0;
while (num_1 < num_2 || num_2 > a)
{
    num_1 += num_1 % num_2;
    num_2--;
    count++;
}
if(count == 3) {
    cout << "Congratulations" << endl;
}
  1. num_1 = 101, num_2 = 99
  2. num_1 = 70, num_2 = 105
  3. num_1 = 95, num_2 = 103
  4. num_1 = 10, num_2 = 20

Q38:

How many times will “hello world” be printed in the following code snippet?

int a = 0;
int b = 3;
for (int c = 9; a < 5 && c >= 0; a++)
{
    c -= b;
    cout << "hello world!" << endl;
}
  1. 3
  2. 4
  3. 5
  4. 6

Answers