Cmpe112 Cmse112 Exercises
Cmpe112 Cmse112 Exercises
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: /
a) 30
b) 20
c) 10
d) 40
switch (ch)
{
case 'a':
case 'A':
printf("1");
}
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
10)
Consider the following c-language declaration:
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
scanf("%s", str1);
gets(str2);
printf("%s", str2);
printf("%s", str1);
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.
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]);
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; }
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) {
char name[80];
int id;
f1 = fopen("data.text", "r");
if(----------------){// statement 1
return (1);}
f2 = fopen("result.text", "w");
while(-------------------){//statement 2
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