Sie sind auf Seite 1von 3

QUIZ 1

CSPP50101-1
1. Which of the following are correct (assuming proper main program setup)?
Mark C (correct) or E (error).
a. char *s = "hello";

b. char *s = "hello";
s = "goodbye";

c. char *s;
s = "hello";

d. char s[] = "hello";

e. char s[];
s = "hello";

f. char s[] = "hello";


s = "goodbye";

g. char s[10];
s = "hello";

h. char s[10];
strcpy(s,"hello");

i. char *s;
strcpy(s,"hello");

j. char *s = "abcde";
strcpy(s,"hello");

answers: a) C b) C c) C d) C e) E f) E g) E h) C i) E j) E
2. What is printed?
a. int main(){
int x;
printf("%d", x);
}
answer: unpredictable (x not initialized)
b. int main(){
int x = 3;
foo(x);
printf("%d", x);
}
foo(int x){
++x;
}
answer: 3 (x passed by value)
c. int main(){
int x[] = {1,2};
foo(x);
printf("%d", x[0]);
}
void foo(int x[]){
x[0]=7;
}
answer: 7 (x passed by reference)
d. int main(){
int x = 3;

foo(x);
printf("%d", x);
}
void foo(int x){
++x;
}
oops, same as b.
e. int main(){
char *s = "hello";
foo(s);
printf("%s", s);
}
void foo(char *s){
s = "goodbye";
}
answer: hello (pointer itself copied in foo)
f. int main(){
char s[100] = "hello";
foo(s);
printf("%s", s);
}
void foo(char s[]){
strcpy(s, "goodbye");
}
answer: goodbye (array name acts like a pointer)
g. int main(){
int i,j;
i=0; j=400;
while (i < j){
--j; ++i;
}
printf("%d", i-j);
}
answer: 0
h. int main(){
int i;
for (i=0; i<100; ++i)
if(i) break;
printf("%d", i);
}
answer: 1
i. int main(){
int i;
for (i=0; i<100; ++i){
if(i) continue;
break;
}
printf("%d", i);
}
anser: 0

Das könnte Ihnen auch gefallen