CS111 SPRING 2020 Lab Quiz 1

 

NAME_________________________

 

CUNYID_______________________

SECTION 1 General 3 Points Each. Answer 4 out of 5. Cross out questions that are not to be graded otherwise first four questions will be graded.

 

1)    What can cause a compile error?

Leaving out a semicolon;declaring same variable name twice in same scope.

 

2)    What could cause the compile error unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)?

If the main() function was not declared

 

3)    Why would a program compiled for a Windows computer not run on an Apple computer?

Because C++ programs are machine language and the CPUs on Windows and Apple computers are different.

 

4)    What does the following line mean? #include <iostream>

It tells the pre-processor to place the contents of the header file called iostream into the current code file.

 

5)    Write the order the three programs below execute.

1) Compiler 2) Linker 3) Pre-Processor e.g. 3) 1) 2)

3) Pre-Processor; 1) Compiler; 2) Linker


 

 

SECTION 2 Variables 3 Points Each. Answer 7 out of 8. Cross out questions that are not to be graded otherwise first seven questions will be graded.

What is the output of the code below?

Q1)

int i = 1, j = 2, k = 6;

double d = 2;

cout << (i / j) * k << " " << (d / j) * k << endl;

 

Q1 0 6____________________

Q2

int i = 473;

cout << i / 100 << (i - ((i / 100) * 100)) / 10 << i - ((i / 10)*10) << endl;

 

Q2 473_________________

 

Q3)

double d = 1.23;

int i = 1.23;

cout << i * 100 +(d-i) << endl;

Q3) 100.23_________________________

 

Q4)

int i = 123 % 100;

cout << i << endl;

 

Q4 23_____________________________

 

Q5)

char c = 'Z';

c--;

cout << c << endl;

 

Q5)__Y____________________________

 


 

Q6)

char c = 'A';

c+=('c' - 'a');

cout << c << endl;

 

Q6) ___C____________________________

Q7)

int i = 10;

i *= (8 + i);

cout << i << endl;

Q7)_180_________________________________________

 

Q8)

cout << ((1 / 2) * 8)

<< " "

<< ((1.0 / 2) * 8) << endl;

Q8)_0 4_________________________________________

 


 

SECTION 3 Programming 10 Points Each. Answer 2 out of 3. Cross out questions that are not to be graded otherwise first two questions will be graded. Your code should begin with the main function.

P1) Write a program that outputs a random letter that exists between two other letters. For example, if A and C are entered, the program will randomly output either A, B, or C.

int main()

{

cout << "Enter two upper case letters and I will"

<< " provide you with a random letter that"

<< " exists between them (inclusive)."

<< " For example 'A' 'C' will result in either"

<< " A, B, or C" << endl;

char first_char, second_char;

cin >> first_char >> second_char;

srand(time(0));

char c = first_char + rand() %

(second_char - first_char + 1);

cout << "The random character is:" << c << endl;

return(0);

}

 

 


 

P2) Write a program that asks the user to enter two whole numbers. Determine whether one is the square root of the other. If they are, print which number is the square root of the other.

int main()

{

cout << "Enter two whole numbers, and I will tell"

<< " you if any number is the square root of"

<< " other" << endl;

int x, y;

cin >> x >> y;

if ((x * x) == y)

cout << x << " is the square root of of " << y << endl;

else if ((y * y) == x)

cout << y << " is the square root of of " << x << endl;

else

cout << "Neither number is the square root of of the other" << endl;

return(0);

}


 

P3) Write a program that asks the user to enter two upper case characters. Print the characters in alphabetic order. i.e. If input is C an B the output should be B C.

int main()

{

cout << "Enter two upper case characters" << endl;

 

char c1, c2;

 

cin >> c1 >> c2;

 

if (c1 >= c2)

cout << c2 << " " << c1 << endl;

else

cout << c1 << " " << c2 << endl;

return(0);

}