0% found this document useful (0 votes)
8 views7 pages

Programming I Code 4

The document consists of a series of programming-related questions divided into multiple parts, including True/False statements, multiple-choice questions, matching columns, and programming tasks. It covers topics such as function declarations, data types, pointers, arrays, and file handling in C++. Additionally, it includes coding exercises that require writing functions and handling user input.

Uploaded by

kidusyoseph448
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

Programming I Code 4

The document consists of a series of programming-related questions divided into multiple parts, including True/False statements, multiple-choice questions, matching columns, and programming tasks. It covers topics such as function declarations, data types, pointers, arrays, and file handling in C++. Additionally, it includes coding exercises that require writing functions and handling user input.

Uploaded by

kidusyoseph448
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Part I: True/False: Say True if the statement is correct, otherwise false. (0.

5 points each)

1. Passing values to a function when the function is called referred to as passing by reference
2. The declaration int *y stores integer value.
3. The string manipulation operator strcpy() used to merge the contents of two string.
4. The continue statement terminates the current iteration of a loop and instead jumps to the next
iteration.
5. You would use structure keyword to define variables of structure type.
6. To read and write from a file we are using the standard C++ library called iostream.
7. The switch statement provides a way of choosing between a set of alternatives, based on the
value of an expression
8. The first element in an array in C++ always has the index 0, and if the array has n elements the
last element will have the index n.
9. Break is used to specify the set of statements to execute if there is no case match
10. In relation of array and pointer, num[n] is the same as *(num+n), where n is integer.
11. Using pointer it is possible to perform basic arithmetic operators like division, addition,
subtraction, and multiplication.
12. An array contains multiple heterogeneous elements with different type stored sequentially in
memory.

Part II: Choose the correct answer for each of the following questions. (1 pt each)

1. What will be the output of the following code fragment


int main()

int num=4;

int *ptr;

ptr=#

cout<<*(&num); }

A. Address of num C. 4
B. 45 D. Error

1|Page
2. One of the following is true statement about string comparison function, strcmp (str1, str2).
A. It returns a positive number if str1 is more than str2
B. It returns a positive number if str2 is more than str1
C. It returns a zero if str2 is more than to str1.
D. It returns a negative number if str2 is less than str1.
3. What will be the output of the following program on screen?
#include <iostream>
using namespace std;
int check (int a, int b, int c)
{ a=10;
b = a++;
c = b%4;
return b*c;}
int main ()
{
int x=3, y=7, z=12;
cout<<check (x, y, z)<<",";
cout<<x<<","<<z << "," << y;
return 0;}
A. 20,3,12,7 C. 20,3,7,12
B. 14,7,3,5 D. Error
4. What is the output of the following program?
#include <iostream>
using namespace std;
int main()
{
char str[7] = "ABCEFG";
str[4]='\0';
cout << str;
return 0;}
A. ABCEFG C. ABCE
B. ABCD D. ABCDE

1|Page
5. Which of the following statements about accessing the members of structure is correct?
struct Student{
int id;
char name[30];
};
Student St, *Ps;
Ps=&St;
A. Ps->id; C. St->id;
B. [Link]; D. *[Link];
6. Choose the right option for the declaration of string *x, y;
A. x is a pointer to a string, y is a string
B. y is a pointer to a string, x is a string
C. Both x and y are pointers to string types
D. None of the above
7. What keyword is used to declare a function that does not return a value?
A. int C. void
B. float D. Return

8. What will be the output of the following code fragment


int add(int num1, int num2 = 7){
return num1 + num2; }
int main(){
cout << add(11);
return 0;}

A. Error C. 7
B. 19 D. 11
9. What will happen in the following code fragment?
int a=3,b=7;
int *p=&b,*q=&a;
p=q;
A. q points to a value 7 C. b has a value 3
B. p points to a value 3 D. a has a value 7
10. What are mandatory parts in the function declaration?
A. Parameters, Variables C. Return Type, Function Name,
B. Parameters, Function Name Parameters
D. Return Type, Function Name
11. Which one of the following loop statement is body is executed first and then the loop condition is
examined.
A. do..while loop C. for loop
B. do… for loop D. while loop

2|Page
12. To declare a two dimensional array of type integer with five columns and 4 rows, you would write:-
A. int array_name[4][5]; C. int array_name[5][5];
B. int array_name[6][5]; D. int array_name[5][4];
13. Which one of the following also kown as jump statement?
A. default C. break
B. goto D. continue
14. Which of the following activate the function?
A. Function Definition C. Function
B. Function Declaration D. Function Call
15. What does the return statement do in a function?
A. Passes control back to the caller D. All of the above
B. Returns a value to the caller E. None of the above
C. Ends the function
Part III Match Column A with Column B(1pt each)

Column A Column B

1. ios::nocreate A. Default Open Modes for write into file


2. Output stream B. Used to read data from a source
3. ios::out C. If the file does not exist, the open operation fail
4. ios::in D. Cause of the file to be open in binary mode
5. Input stream E. Default Open Modes for read from file

. F. Used for writing data to a destination

G. If the file exists, the open operation fail


H. Used to read data from a destination

I. Used for writing data to a destination

Part IV Answer the following question

1. Define the following term(1pt each question)


A. Reference operator
B. Pointer to Pointer
C. Dereference operator
D. Recursion

2|Page
E. Aliasing
Part V Write the Output of the following Program()

1. 2pt

#include <iostream>
using namespace std;
int main(){
for(int i=4;i<=30;i++){
if(i%2==0||i==15)
continue;
else if(i==13)
break;
else
cout<<i<<" , ";
}
return 0;}

2. Assume the memory address of the first element in the array is 1000 (2pt)

#include<iostream>
using namespace std;
int main() {
int n[5]={3,27,66,16,19};
int * p;
p=n;
for (int i=4; i>=0; i--) {
cout<<*(p+i)+5<<" ";}
cout<<endl;
for (int i=4; i>=0; i--)
{
cout <<*p+i<<" ";
}
return 0;}

2|Page
3. 3pt

#include <iostream>
using namespace std;
void fun1(int,int);
void fun2(int&,int);
void fun3(int&,int&);
void display(int,int);
int main(){
int a=4;
int b=7;
display(a,b);
fun1(a,b);
display(a,b);
fun2(a,b);
display(a,b);
fun3(a,b);
display(a,b);
return 0;
}
void fun1(int x,int y)
{
x=x*3;
y=x+6;
display(x,y);
}
void fun2(int& x,int y)
{
x*=2;
y/=6;
display(x,y);
}
void fun3(int& x,int& y)
{
x=y/9;
y+=4;
display(x,y);
}
void display(int n1,int n2)
{
cout<<n1<<" : "<<n2<<endl;
}

3|Page
4. 2pt

#include<iostream>
#include <string.h>
using namespace std;
int main(){
char x[]= "my";
char y[]="name";
strcpy(x,"do not copy this");
strncpy(y,x,6);
x[2]='\0';
cout<<y<<endl;
cout<<x<<endl;
cout<<strcmp("Aabx","abef")<<endl;
cout<<strcmp("AdF","Abac");
return 0;
}

Part VI: Write the program of the following question


1. Write a function named mult() that accepts two floating-point numbers as a parameters, multiplies these
two numbers and display the result.(2pt )
2. Introduce int variables x and y and int pointer variables ptr1 and ptr2. Set x to 9, y to 12, p to the
address of x, and q to the address of y. ( Assume the memory address of the x is 10xx, for y is 20yy, for
ptr1 is 30ptr, for ptr2 is 40ptr2) Then print the following information: (2pt)
A. The address of x and the value of x.
B. The value of ptr1 and the value of *ptr1.
C. The address of y and the value of y.
D. The value of ptr2 and the value of *ptr2.
3. Create one dimensional array and accept the element from the user and to find the largest and smallest
elements in a one dimensional integer array using pointer(Note the array size accepted from user. This
means how many elements accepts are define by user).(3pt)
4. ABC Company on of the most popular book publisher and seller in Addis Ababa. ABC Company need
store the information of the book in stucture using file handling method. The detail information of the
book is book_title, author_name, book_price, book_code and book_publish_year. Based on this scenario
answer the following question(Rember: the number of the book is n and all the information accept
from the user and the file are store in structure)
A. Write those detail information in the file(2.5pt)
B. Read the file(2.5pt)

4|Page

You might also like