0% found this document useful (0 votes)
60 views3 pages

Past Paper 2024

The document contains a series of programming questions and answers related to loops, arrays, and functions in C++. It explains the differences between pre-tested and post-tested loops, provides examples of converting a for loop to a do-while loop, and discusses two-dimensional arrays and the sizeof() function. Additionally, it includes code snippets for calculating the sum of an array, using inline functions, and analyzing an array's elements.

Uploaded by

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

Past Paper 2024

The document contains a series of programming questions and answers related to loops, arrays, and functions in C++. It explains the differences between pre-tested and post-tested loops, provides examples of converting a for loop to a do-while loop, and discusses two-dimensional arrays and the sizeof() function. Additionally, it includes code snippets for calculating the sum of an array, using inline functions, and analyzing an array's elements.

Uploaded by

Zainab Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Past paper 2024

Q1) Write down differences between pre-tested and post-tested loops. Give example.
Ans)

Pre-tested loop Post-tested loop


 Pre-tested loop is in which the  Post-tested loop is in which the code
condition is first read and then is first executed and then checks the
executed. condition applied in the code.
 While loop is a pre-tested loop  Do-while is a post-tested loop as the
because the condition is checked condition is checked after executing
before executing the program. the program.
 The body statement will not be  The body statements will execute at
executed if the condition does not least once even if the condition is
apply. false.

Q2) Write the following for loop into do-while loop.


for(int n=0;n<10;n++)
cout<<n<<” “;
Ans) do{
cout<<n<<” “;
n=n+1
}
while(n<10);
Q3) Briefly explain the concept of two-dimensional array with an example.
Ans) A dimensional array uses a single variable to represent a collection of same data type that
is in the the form of table. Two-dimensional array has two dimensions i.e. vertical and
horizontal dimensions.
Example:
Column 0 column 1 column 2 column 3 column 4

Row 1 a[0][0] a[0][1] a[0][2] a[0][3] a[0][4]


Row 2 a[1][0] a[1][1] a[1][2] a[1][3] a[1][4]
Row 3 a[2][0] a[2][1] a[2][2] a[2][3] a[2][4]
Q4) Write down the purpose of sizeof ( ) function in array.
Ans)
 The sizeof () function is used to give the number of bytes occupied to store values for
data type within its parenthesis.
 Sizeof () function is used to determine the amount of storage used to store int, char,
float, etc.
 This function can also be used to find the number of bytes reserved for an array.
Example:
In an array, in a[5]. To determine the stored bytes for this we will use sizeof(a). This will return
20 because each element of integer array occupies 4 bytes and there are 5 elements.
Q5) Write down the output of the following code-segment.
int p,q,r;

p=10; q=3;

if(p%q==3)

r=0;

else

r=1; cout<<r;

Ans)

The output of this program will be:

r= 1.

Q6) Write a c++ code that prints sum of following.

int arr [10] = {1,2,3,4,5,6,7,8,9,10};

Ans)

void main(){

int arr[10] = {1,2,3,4,5,6,7,8,9,10};

sum=0;

for(int i=0;i<10;i++){

sum=sum+arr[10];

cout<<”\nSum of arr[10] is “<<sum;


}
Q7) Write down a piece of code in c++ that shows the use of inline function.

Ans)

inline int min (int a, int b) {

return a>b? b: a;

void main () {

cout<<” Minimum out of 13 and 32 is:” <<min (13,32);

cout<<” Minimum out of 34 and 65 is:” <<min (34,65);

getch();

Q8) Given the array definition:

float a[5]={1,2,3};

a. How many elements are there in the array?

b. What are the values of the first and last elements?

Ans)

 Elements: 3(1,2,3)
 First element: 1
 Last element: 3

You might also like