Professional Documents
Culture Documents
Final Exam 2023 Code I-1
Final Exam 2023 Code I-1
Final Exam 2023 Code I-1
1
Instructions:
Having a cell phone at hand/in the pocket, extra paper is not allowed and will lead to
denial of the examinee to take the exam.
Write your name, ID number and section in all appropriate places.
The Exam has five parts: I (5%), II (15%), III (5%), IV (10%), and V (15%)
Accordingly, Attempt all questions.
Your answers need to be neat and clear, and put them only on the answer sheet.
Cheating or any attempt to cheat will make your value zero and result in other sanctions
as may be authorized by the university.
Student Description:
Id Number: ___________________________
Section: ___________________________
Part I: Write “True” if the statement is correct and “False” if the statement is incorrect. (1pt each=5pts).
1. In nested if else, if a condition is false, the statement associated with it is executed and it
terminates the whole if-else chain.
2. A function cannot be defined inside another function.
3. With the declarations:
a. int i = 10;
int* ptr = &i;
the following statement will set the value of the variable i to 11:
*ptr = 11;
4. The following two functions can coexist in the same C++ program.
a. int foo(const char ch);
2
b. bool foo(int &x, int &y);
5. strlen("Cletus") is 7 because of the null character ('\0') that ties on the end of 'Cletus'.
Part II: Choose the best answer among the given alternatives (1pt each=15pts)
3
A) 10 B) 11 C) 12 D) Error
6. How many times CppBuzz.com is printed in the following program?
A) Error
B) 5 times
C) 4 times
D) 6 times
7. What is the output of the following program?
A) 111011
B) 111111
C) 101011
D) 101010
4
A) 10
B) 20
C) compile time error
D) 30
E) 53
9. Which one of the following is true about the following slice of C++ code?
if (x>z)
if (x>y) cout<<x; else cout<<y;
else if (y>z) cout<<y; else cout<<z;
A) prints out 7 for x=7, y=6 and z=9
B) prints out 6 for x=7, y=6 and z=9
C) prints out nothing for x=0, y=0 and z=0
D) prints out 97 for x=A, y=a and z=B
10. What is the correct sequence of execution for a for loop statement?
A) initialization, condition, update, body
B) initialization, body, condition, update
C) initialization, condition, body, update
D) initialization, update, condition, body
11. What is the output of the following code?
#include<iostream>
using namespace std;
#include<string.h>
int main()
{
cout<<strlen("Hello, World.\n")<<"\n";
}
(A) 14 (B) 13 (C) 12 (D) None
12. Sending a copy of data to a program module is called __________
(A) recursion (B) passing a reference (C) passing a value (D) None
5
D) int *pi = 0;
Part III. Give short answers for the following questions based on the instruction (5pts)
1. Write a declaration statement of an integer variable x pointed by ptr. (1 pts)
2. Describe the difference between pass by value and pass by reference. (2 pts)
3. What is the difference between do…While loop and While loop (2pts)
Part IV. Write the output for the following fragment of codes (2 pts each=10pts)
#include <iostream>
using namespace std;
int main()
{
int i, j;
int val[3][4]={8,7,2,5,3,10,7,6,1,51,22,100};
for (i=2;i<3;i++)
for(j=0;j<1;j++)
cout<<” “<<val[i][j];
return 0;
}
2. Write the output for the following programs
(A) (B)
sum=0; sum=0;
for (i=5;i>=0;i--){ for (i=5;i>=0;i--){
if(i==3){ if(i!=3){
break; continue;
sum=+i; sum+=i;
} }
} }
cout<<sum; cout<<sum;
6
4. What is the output of the following code?
#include <iostream>
using namespace std;
int operate (int a, int b){
return (a * b);
}
float operate (float a, float b){
return (a / b);
}
int main(){
int x = 5, y = 2;
float n = 5.0, m = 2.0;
cout << operate(x, y) <<"\t";
cout << operate (n, m);
return 0;
}
5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void copy (int a, int& b, int& c){
a *= 2;
b *= a;
c *= b;
}
int main (){
int x = 1, y = 3, z = 7;
copy (x, y, z);
cout << "x =" << x << ", y =" << y << ", z =" << z;
return 0;
7
}
pts)
4. Define a structure that represents Fruit with properties fruit name, fruit type, fruit color.
Write a program that accepts data of four fruits and displays the results. (4 pts)