Final Exam 2023 Code I-1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Jimma University

Jimma Institute of Technology


Computer Programming (ECEg1052)

Beneficiary: Department of Pre-Engineering Program: Regular


Academic Year: 2022/23 Year: I Semester: II
Exam Weight: 50% Exam Date: 02/10/2023 Time Allowed: 2:30
Hours
Exam code: 0001

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:

Full Name: ___________________________

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)

1. One of the following is NOT true


A) when the result of the condition is true in an if else block, the body of the else statement
is never executed.
B) when result of the condition is false, the body of the if statement is executed.
C) case keyword is used to identify possible entry points of switch statement.
D) In do while, its body is executed first and then the loop condition is examined.
E) If expression 2 is omitted in for loop, C++ assumes that the continuation-test condition is
true which results in a forever loop condition.
2. One of the following statements is used for specifying alternate paths of execution, depending on the
outcome of a logical condition.
A) If statement
B) For loop
C) while loop
D) All
3. Which one of the following is true about array?
A) Assignment operator can be applied to array variables
B) A two-dimensional array has three subscripts
C) The number of elements in an array must be fixed at compile time
D) Copying the whole element of one array to another array in C++ is impossible

4. What is the output of the given program?

A) -20 B) -40 C) -80 D) 40


5. What is the output of the following program?

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

8. What is the output of the following program?


#include < iostream >
using namespace std;
void fun(int x, int y){
x = 20;
y = 10;
}
int main(){
int y = 20;
int x = 10;
fun(x, y);
cout << y;
return 0;
}

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

13. Which of the following correctly declares an array in C++?


(A) array{10}; (B) array array[10]; (C) int array; (D) int array[10];

14. What are mandatory parts in the function declaration?


(A) return type, function name (B) return type, function name, parameters
(C) parameters, function name (D) parameters, variables
15. Which of the following is illegal?
A) int *ip;
B) string s, *sp = 0;
C) int i; double* dp = &i;

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)

1. Determine the output produced by the following program.

#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;

3. What is the output of the following program?

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
}

Part V. Write a C++ program for the following questions (15pts)


1. Write a program that calculates and displays the income tax of a salary entered from the
keyboard. Your program must calculate taxes according to the following rates: (4 pts)
Up to 200Br 0%
200Br – 600Br 10%
600Br – 1200Br 15%
1200Br – 2000Br 20%
2000Br – 3500Br 25%
3500Br & above 30%
For example: The income tax of a gross salary of 900 Birr is 85 Birr, which is calculated as
(200*0% + 400*10% + 300*15%)=85 Birr.
2. Write a program that accepts a list of marks of students and counts the number of students who
have scored marks less than 45 in an exam out of 60. The total number of students who took the
test is to be entered from the keyboard. (3 pts)
3. WriteOUTPUT:
SAMPLE a C++ program which has two functions (namely odd and even). The odd function
Enter two numbers:
calculates the10
sum of all odd numbers between the two numbers entered by the user. The even
20 function calculates the sum of all even numbers between the two numbers entered by the user. (4
The sum of odd numbers is:
The sum of even numbers is:

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)

You might also like