In: Computer Science
(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 choices
True
False
Flag this Question
Question 3 10 pts
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; }
Flag this Question
Question 4 10 pts
What is the output of the following code?
void myFunc(int n) { n = 10; } int main() { int num = 4; myFunc(num); cout << num; }
Flag this Question
Question 5 10 pts
What is the output of the following code?
void myFunc(int &n) { n = 10; } int main() { int num = 4; myFunc(num); cout << num; }
Flag this Question
Question 6 10 pts
What is the output of the following code?
int myFunc(int n) { n = 10; return n; } int main() { int n = 4; cout << myFunc(n); }
Flag this Question
Question 7 10 pts
(True or False) The following is an infinite loop.
int main() { bool flag = false; int num = 0; while(!flag); { cin >> num; if(num == -1) { flag = true; } } return 0; }
Group of answer choices
True
False
Flag this Question
Question 8 10 pts
What would the output of the following code be?
for(int i = 0; i < 5; ++i) { for(int j = 0; j < i; ++j) { cout << j; } }
Flag this Question
Question 9 10 pts
Given the following input:
12.34.a
What would the output of the following be?
int i; float j; char ch; cin >> i >> j >> ch cout << i << " " << j << " " << ch;
Q1
Answer: False
The function's return type is void. But the function is returning a value. This will lead to an error, and thus, the program will not compile.
Q2
Answer: True
Because the length of "before" is greater than the length of "back". Therefore, the if condition of the code gets true value from the isGreater function, thus leading to execute the if part of the code.
Q3
Output: 0123401234
The outer loop will run 2 times for i=1 and i=2.
For each iteration of outer loop the inner loop will run 5 times with the value of j being 0,1,2,3,4.
Thus the output will be 0123401234.
Q4
Output: 4
The line
myFunc(num);
has no effect on the value of the variable num. Because the value is passed as pass by value. Therefore, the changes made will have scope only till the function myFunc exists. After that the previous value of num will continue.
Therefore, the value of num remains 4 and thus 4 is printed as output.
Q5
Output: 10
As the value is passed as pass by reference that is the address of the variable num is passed, the modifications made in the function will be having effect in the main function also.
Therefore, the value of num becomes 10 and is displayed as output.
Q6
Output: 10
The function myFunc is returning value of n=10, which will be displayed by the cout function in the main.
Therefore, 10 will be the output.
Q7
True
The while statement has been ended with a semicolon thus resulting the loop to fall in infinite state.
Q8
Output: 0010120123
i=0
j=0
output: 0
i=1
j=0
output: 00
i=1
j=1
output: 001
i=2
j=0
output: 0010
i=2
j=1
output: 00101
i=2
j=2
output: 001012
i=3
j=0
output: 0010120
i=3
j=1
output: 00101201
i=3
j=2
output: 001012012
i=3
j=3
output: 0010120123
Q9
The given program has an error in the line
cin >> i >> j >> ch
The line misses a semicolon in the end.
Had the line been
cin >> i >> j >> ch;
then the output would had been
12 0.34 .