0% found this document useful (0 votes)
668 views8 pages

PF Final Term Exam Fall 2023 Soultion

The document contains the final term exam questions for the CS2103 Programming Fundamentals course at Sir Syed C@SE Institute of Technology, Islamabad. It has 10 questions worth a total of 100 marks. The questions cover topics like C programming fundamentals, arrays, strings, functions, loops, pointers and 2D arrays. Sample code and expected outputs are provided for dry running and testing program logic.

Uploaded by

Yasir Niazi
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)
668 views8 pages

PF Final Term Exam Fall 2023 Soultion

The document contains the final term exam questions for the CS2103 Programming Fundamentals course at Sir Syed C@SE Institute of Technology, Islamabad. It has 10 questions worth a total of 100 marks. The questions cover topics like C programming fundamentals, arrays, strings, functions, loops, pointers and 2D arrays. Sample code and expected outputs are provided for dry running and testing program logic.

Uploaded by

Yasir Niazi
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

Sir Syed C@SE Institute of Technology, Islamabad

Final Term Exam – Fall 2023


CS2103 Programming Fundamentals
Total Marks: 100 Time Allowed: 180 mins.
Q Marks
1). Write the Output of following programs 10
a) int n,s1,s2;
s1=2;s2=100; Output:
for(n=5;n<=9;n=n+1)
{
if(n%2!=0) S1 23 s2 114
s1=s1+n;
else
s2=s2+n;
}
cout<<s1<<” “<<s2;
b) int arr[20]={5,10,20,30,40,50,60,70,80,90};
Output:
int a;
5
fun(&arr[2]);
for(a=0;a<10;a++) 10
cout<<arr[a]; 20
void fun(int *p) 30
{ 40
p=p+1; 50
*(p+4)=*(p+3)+ *(p+1); 90
P[3]=p[2]+p[1]; 90
p=p+2; 90
p[3]=p[4]; 90
p[2]=*(p+3);
}
2 . part [Link] run the code and write the output of following programs a (10)
char arr[20]={“abgts012345"}; w
for( index =1; index <6; index=index+2) g
{ arr[index]=arr[index +2]+3; 3
} s
for(index =0; index <10; index++) 5
{
cout<<arr[index]<<endl; 1
} 2
3
4

Page 1/4
2. part b. Dry run the code and write the output of following programs (10)
int main()
{ 12
for(int index=1; index-3; index++)
cout<<index;
}

Page 2/4
3. Part a. Dry run the code and write the output of following programs (10)
char arr[10][30]=
{
{"abcdef"},
{"ghijk"},
{"012345"},
{"lmnop"},
{"qrstuuv"}
};
int rindex; abcdef
strcat ( &arr[3][3], &arr[2][1] ); ghijknop12345
strcpy ( &arr[4][2], &arr[0][1] ); 012345
strcat ( &arr[1][2], &arr[3][2] ); lmnop12345
strcpy ( &arr[4][1], &arr[3][4] ); qp12345
for(rindex=0; rindex <5; rindex ++)
{
puts(&arr[rindex][0]);
}
3. b. int arr[3][3]={{0,0,0},{0,0,0},{0,0,0}};
for(row=0;row<2;row++);
{
for(int col=0;col<2;col++)
{ 0 0 0
arr[row][col]=row*col; 0 0 0
} 0 2 0
}
for(row=0;row<3;row++)
{ for(int col=0;col<3;col++)
{
cout<<arr[row][col];
}
cout<<endl;
}
[24 Marks] (10)
4. Part a
void main()
{
char ch[16]="xyz is best";
ch[13]='c';
puts(ch);
}
what is the output of the program?
4. Part b
What would be the output of the following programs:
int n[3][3] = {2, 4, 3,
6, 8, 5,
3, 5, 1} ;
Cout<<n[3][3]<<n[2][2] ;
(A)Garbage value (B) Compilation error
(C) Run time Error will occur and output will be Garbage 1
Page 3/4
4. Part c.
char arr[2][20], temp[]={“This is a quiz”};
strcpy(&arr[0][0], &temp[0]);
strcpy(&arr[1][0],&temp[5]);
for(int i=0;i<2;i++)
puts(&arr[i][0]);

The output will be:


(A) This is a quiz (B) This is a quiz (C) This is a quiz
This is a quiz i is a quiz is a quiz

4. Part d. Write the output of the program


int d,a,b,c;
a=1;
b=2;
c=3;
d=(b=3)&&(a+2)&&(a-c);
cout<<a<<b<<c<<d;

4. Part e. what will be the output of the program?


char ch1[]="byee“; Un equal
char ch2[]="byee";
if(ch1==ch2)
printf("equal");
else
printf("unequal");
1) Equal 2) unequal

5. Part a Write the output of the program (10) 1231


int d,a,b,c;
a=1; b=2; c=3;
d=(a-c)||(a-1)&&(b=5);
cout<<a<<b<<c<<d;

5. Part b. What will be the output of the program ?


a=5;
if( (a=7)&&(a>=6) )
cout<<" i am if body”<<a;
else
cout<<" i am else body"<<a;

5. Part c. What will be output if you will execute following c code?

int arr[][3]={{1,2},{3,4,5},{5}};
cout<<arr[0][2]<<arr[1][2];

(A) 04 (B) 15
(C) 05 (D) 1 15 (E)Garbage
Page 4/4
5. Part d. What will be the output of the following program:
int j, k= 20 , l= 0;
for (int k = 0; k < 5; k++)
{
if (k == 4)
{
k = 10;
}
}
cout<<k;
(A). 20 (B). 4 (C). Compilation error (D).Syntax error.
5. Part e. Consider this program segment
int arr[]={5,12,31,40,52,61,17,81,19,100};
for (int i =0; i<=2*k+1; i++)
sum=sum + arr[i];
For what value of k for the above program which will compute the sum of array?
(A) 10 (B) 4 (C) 5 (D) None of the above

6. Write a program to read marks of 4 students in 3 quizzes and find the average of each quiz. (use 2D
array) [10 ]
Quiz
10 20 20
10 20 10
Student
20 30 40
20 30 50
Average 15 25 30

Solution:
int main()
{
int index, max, smax;

int arr[5][5];
int sum=0;
float avg[10]={0.0};
for (int col = 0; col < 3; col++)
{
sum=0;
for (int row = 0; row < 4; row++)
{
sum=sum+arr[row][col];
} // end of col loop
cout << endl;
avg[col]=sum/4;
} // end of outer loop
return 0;
} // end of main function

Page 5/4
7. Write a program that takes 10 integer number from user and displays the second highest
number.[10 ]

Solution:

int main()
{
int index, max, smax;
int arr[10] = {5, 34, 23, 34, 45, 45, 34, 5, 6, 7};

max = arr[0];
smax = arr[0];
for (int index = 0; index < 10; index++)
{
if (arr[index] > max)
{
smax = max;
max = arr[index];
}
else if (arr[index] > smax)
{
smax = arr[index];
}
}
cout << smax;

return 0;
} // end of main function

5 10 34 45 3 15 66 7 8 78

8. Write a program that reads a string from user and then convert all the capital characters into small
and all the small letters into capital. [10 ]
Example: Input AbCdEf
Output aBcDeF

int main()
{
char arr[20]="AbczDezF";
for(int index=0;arr[index]!='\0';index++)
{
if (arr[index] >= 'A' && arr[index] <= 'Z')
{
arr[index] = arr[index] + 32;
}
Page 6/4
else if (arr[index] >= 'a' && arr[index] <= 'z')
{
arr[index] = arr[index] + 32;
}
} // end of string loop

return 0;
} // end of main function

9. Write a program that should sort the list of names according to length of the string. [10 ]

void mysort_strlen(char arr[][10], int nrow)


{
for (int counter=1;counter<nrow-1;counter++)
{
for (int row=0;row<10;row++)
{
int l1,l2;
l1 = strlen(&arr[row][0]);
l2 = strlen(&arr[row+1][0]);
if (l1>l2)
{
myswap(&arr[row][0], &arr[row + 1][0]);
}
} // end of row loop
} // end of outer loop
}

10. Write the program using nested loop to find the sum of the following series. [10]

int main()
{
int sum = 0;
for (int j = 1; j <=2; j++)
{
for (int k = 1; k <=3; k++)
{
sum = sum + (k*k*k)/j;;
} // end of string loop
}
return 0;
} // end of main function

Page 7/4
Page 8/4

You might also like