0% found this document useful (0 votes)
67 views

Cmpe112 Cmse112 Exercises

The document contains 15 multiple choice questions related to C programming concepts such as operators, control flow, functions, arrays, pointers, strings, file I/O, and more. The questions test understanding of basic syntax, evaluating expressions, analyzing code snippets, and identifying missing code to accomplish tasks related to string manipulation, array processing, and reading/writing files.

Uploaded by

Nouaim Elaakil
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)
67 views

Cmpe112 Cmse112 Exercises

The document contains 15 multiple choice questions related to C programming concepts such as operators, control flow, functions, arrays, pointers, strings, file I/O, and more. The questions test understanding of basic syntax, evaluating expressions, analyzing code snippets, and identifying missing code to accomplish tasks related to string manipulation, array processing, and reading/writing files.

Uploaded by

Nouaim Elaakil
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
You are on page 1/ 9

CMPE112-CMSE112 additional Questions

1- What are the final values of variables x and y in the code below?

int y = 8;
int x = 0 == 2 && y++;

a) x= 0, y=9
b) x=1, y=8
c) x=0, y=8
d) x=1, y=9

2- The output of the following code is 2. Choose appropriate operators for op1 and op2
in the code given below.

int x = 2, y = 2;
x op1 x op2 y;
printf("%d\n", x);

a) op1: /= , op2: /
b) op1: = , op2: /
c) op1: /= , op2: %
d) op1: += , op2: /

3- What is the final value of x in the code below?

int x = 5 > 8 ? 10 : 1 != 2 < 5 ? 20 : 30;

a) 30
b) 20
c) 10
d) 40

4- Which of the following correctly replaces the code below?

switch (ch)
{
case 'a':
case 'A':
printf("1");
}

a) if (ch == ‘a’ || ch == ‘A’) printf(“1”);


b) if (ch == ‘a’ && ch == ‘A’) printf(“1”);
c) if (ch == 'a') if (ch == 'A') printf("1");
d) none of the mentioned
5- The following function is intended to be used for exchanging the values of two
integers received from the main program:

void exchange (int a, int b)


{
int temp;
temp = a;
a = b;
b = temp;
}

Which of the following is true?

a) The function call statement is: exchange (x,y);


b) The function call statement is: exchange (&x,&y);
c) exchange (x,y) cannot be used because its return type is void
d) exchange (x,y) cannot be used because its parameters are passed by value

6- What is the output of the following program?

void foo(int n, int sum)


{
int k = 0, j = 0;
if (n == 0) return;
k = n % 10;
j = n / 10;
sum = sum + k;
foo (j, sum);
printf ("%d,", k);
}

int main ()
{
int a =4307, sum = 0;
foo (a, sum);
printf ("%dn", sum);
}

a) 70340
b) 703414
c) 43070
d) 430714
7- What is the output of the following program?

#include <stdio.h>
void f(int a[][3])
{
a[0][1] = 3;
int i = 0, j = 0;
for (i = 0; i < 2; i++)
for (j = 0; j < 3; j++)
printf("%d", a[i][j]);
}
void main()
{
int a[2][3] = {1};
f(a);
}

a) 130000
b) 030000
c) 101131
d) 100000

8- After the execution of the following code, the elements of array a are 0,1,3 in order.
Which of the following replaces the missing code?

main()
{
int a[3] = {0,1,2},i,j;
for(i=1; i < 3; i++)
for(j=1; j < 3; j++)
____________________________ //missing code
for(i=0; i < 3; i++)
printf("a[%d] = %d\n", i, a[i]);
}

a) a[j]+=i/j;
b) a[j]+=i%j;
c) a[j]+=i+j;
d) a[j]+=i-j;
9)
What would be the value of the expression
strcat("Welcome","again")
A. NULL

B. The string "againWelcome"

C. The string "Welcomeagain"

D. The statement is invalid

10)
Consider the following c-language declaration:

char s1[30] = "hi", s2[20] = "world", s3[10]= "";

Then, the correct way to have string "hiworld" in s3 is

A. s3 = s1 + s2;
B. strcpy(s3,s1); strcat(s3,s2);
C. strcat(s3,s1); strcpy(s3,s2);
D. strcpy(s3,s1); strcpy(s3,s2);

11)
What would be the output of the following c-code?

#include<stdio.h>
int main(){
char s[] = "Hello\0Hi";
printf("%d %s", strlen(s),s);
return 0;}

A. 5 Hello
B. 5 Hello\0Hi
C. 9 Hello\0Hi
D. 8 Hello\0Hi
12)
What would be the output of the code segment given below if the input is
ISTANBUL IS COLD TODAY

char str1[81], str2[81];

scanf("%s", str1);

gets(str2);

printf("%s", str2);

printf("%s", str1);

A. ISTANBUL IS COLD TODAY


B. IS COLD TODAY
C. IS COLD TODAYISTANBUL
D. IS COLD TODAY ISTANBUL

13)
The function reverse given below receives a string and reverses it. For
example if the received string is “ABCD” then it becomes “DCBA”. What would
be the missing statements to accomplish this task.
void reverse (char st[]) {
int i, k;
char temp;
k = strlen(st);
for(i=0; i < k/2; i++) {
temp = st[k-i-1];
__________________ // Statement 1
_____________________// Statement 2
}
}
A. Statement 1 is st[i] = temp;
Statement 2 is st[k-i-1] = st[i];
B. Statement 1 st[k-i-1] = temp;
Statement 2 is temp = st[i];
C. statement 1 is st[k-i-1] = st[i];
statement 2 is st[i] = temp;
D. Statement 1 is st[i] = st[k-i-1];
Statement 2 is st[k-i-1] = temp;

14)
Given the following function prototype:
void FAS(double a[],double b[],int size,double *p,double *e);

Which of the function calls below is correct for the above function prototype?
Assume x and y are 1-D arrays of type double. Assume SIZE is integer, and
sum and average are variables of type double.

A. FAS( x, y, SIZE, &sum, &average);

B. FAV( x[SIZE], y[SIZE], SIZE, &sum, &average);

C. FAS( &x, &y, SIZE, &sum, &average);

D. FAS( x, y, SIZE, sum, average) ;

15)
Consider the following incomplete code fragment:
int x[3]={2,5,9};
______________________// call the function fun
printf("%d %d %d\n", x[0],x[1],x[2]);

where the function fun is defined as follows:


void fun (int *a, int b, int *c) {
*a = *a + 2;
b = b - 1;
*c = *c + 1;
}

which one of the following is the correct call for the function fun to generate the
output
2 6 11
A. fun(&x[1], x[0], &x[2]);
B. fun(&x[2], x[0], &x[1]);
C. fun(&x[0], x[1], &x[2]);
D. fun(x, x[0], x);
16)
Given the following c-code:
#include <stdio.h>
int main() {
int a=2,b=3,c;
int *p1,*p2;
p1=&a;
p2=&b;
*p1=2+*p2;
*p2=a+3;
c=a+b;
printf("%d %d %d",a,b,c);
return 0; }

Then, the correct output is:

A. 3 5 8
B. 5 8 13
C. 8 13 5
D. 2 8 13

17)
What is the output after executing the following code?
void f1(int *x, int a, int b);
int main (){
int x = 10, i, a = 0, b = 1;
for (i = 0; i < 2; i++)
f1(&x, a, b);
printf("%d %d", x, b);
return 0; }
void f1(int *x, int a, int b){
*x = a + b;
b++;
}
A. 10 2
B. 1 1
C. 10 1
D. 1 2

18)
The following code reads the inputs (Student ID, Student Name, Scores of
Quiz, Lab and Exam) from a file (data.text) and then computes and outputs
the total score of each of the student, to one digit after the decimal point, in
another data file named (result.text). What would be the missing statements
to accomplish this task?
#include <stdio.h>

#include <string.h>

int main(void) {

FILE *f1, *f2;

char name[80];

int id;

double quiz, lab, exam, total;

f1 = fopen("data.text", "r");

if(----------------){// statement 1

printf("Error opening the file\n");

return (1);}

f2 = fopen("result.text", "w");

while(-------------------){//statement 2

total = quiz + lab + exam;

fprintf(f2, "%d %s %.1f\n", id, name, total);}

fclose(f1);
fclose(f2);

return (0);}

A. Statement 1 is f1 = NULL
Statement 2 is fscanf(f1, "%d%s%lf%lf%lf", &id, name, &quiz, &lab,
&exam) = EOF

B. Statement 1 is f1 == NULL
Statement 2 is fscanf(f1, "%d%s%lf%lf%lf", &id, name, &quiz, &lab,
&exam) != EOF

C. Statement 1 is f1 = EOF
Statement 2 is fscanf(f1, "%d%s%lf%lf%lf", &id, name, &quiz, &lab,
&exam) = NULL

D. Statement 1 is f1 == EOF
Statement 2 is fscanf(f1, "%d%s%lf%lf%lf", &id, name, &quiz, &lab,
&exam) != NULL

You might also like