C Language - Chapter 5
What will be displayed by the call print('*', 4) based on the implementation of the following method? void print(char symbol, int num) { while(num > 0) { printf("%c\n", symbol); num--; } }
* * * *
What is the output of the following code? #include <stdio.h> int mystery(int x, int y) { return x * y; } double mystery(double x, double y) { return x - y; } int main() { float m = 3; double n = 4; printf("%.1lf", mystery(m, n)); }
-1.0
What's the value of fib(3) based on the following code segment? long fib(long n) { if (n < 1) { return n; } else { long fib1 = fib(n - 1); long fib2 = fib(n - 2); return fib1 + fib2; } }
-2
What is the output of the following program? void getMax(int num1, int num2, int max) { if (num1 > num2) { max = num1; } else { max = num2; } } int main() { int max = 0; getMax(1, 2, max); printf("%d", max); }
0
What's the output of the following code segment: int main() { int num = 3; func(num); } void func(int x) { if (x > 0) { func(--x); printf("%d\n", x); func(x--); } }
0 1 0 2 0 1 0
What's the output of the following code segment? int main() { printf("%d\n", fib(3)); } int fib(int n) { if (n <= 1) { printf("%d\n", n); return n; } else { int fib1 = fib(n - 1); int fib2 = fib(n - 2); printf("%d\n", fib1 + fib2); return fib1 + fib2; } }
1 0 1 1 2 2
What's the output of the following code segment? int main() { factorial(4); } int factorial(int n) { if(n <= 1) { printf("%d\n", n); return 1; } else { int res = n * factorial(n - 1); printf("%d\n", res); return res; } }
1 2 6 24
What is the output of the following code? void a(double b) { b++; printf("%.1f", b); } int main() { a(10); }
11.0
What is the output of the following code? void a(double b) { b++; printf("%lf", b); } int main() { a(10); }
11.0
What is the output of the program? void method(int i, int num) { int j; for (j = 1; j <= i; j++) { printf("%d ", num); num *= 2; } printf("\n"); } int main() { int i = 1; while (i <= 5) { method(i, 2); i++; } }
2 2 4 2 4 8 2 4 8 16 2 4 8 16 32
After invoking method printStars, what is the value of n in the main method? void printStars(char symbol, int count) { while(count > 0) { printf("%c\n", symbol); count--; } } int main() { int n = 3; printStars('*', n); }
3
How many stars are displayed after the execution of the following code segment? long fib(long n) { printf("*"); if (n <= 1) { return n; } else { long fib1 = fib(n - 1); long fib2 = fib(n - 2); return fib1 + fib2; } }
5
What is the output of the following code segment? void a(int b) { b++; } int main() { int c = 5; a(c); printf("%d", c); }
5
What is the output of the following code? void a(int b) { b++; } int main() { int c = 5; a(c); printf("%d", c); }
5
What is the output of the following code? void m(double a) { printf("%f, a"); } void m(double b) { printf("%f", a*2); } int main() { m(2); }
Because the two methods have the same signature, the program has a compile error.
assume that a is defined another file and will be linked with the above code, which of the following statement is true? #include <stdio.h> int main() { extern a; printf("%d\n", a); }
Compile error because datatype of a is missing.
Which of the following statements are true about the following code segment? int main() { int num = 3; func(num); } void func(int x) { if (x > 0) { func(x--); printf("%d\n", x); func(x--); } }
It will result in infinite recursive calls.
For the following two methods, which one will be invoked by the statement double z = m(5, 6.4);? Method 1: int m(double x, double y) Method 2: int m(int x, double y) Method 3: int m(int x, int y)
Method 2
What is the scope of the variable declared in a user defined function?
Only inside the {} block that encloses the variable.
What does a method signature include?
The list of the parameter types method name
Based on the implementation of the following methods int calcLog(int num) { int res = 0; while(num > 0) { num = num / 2; res++; } return res; } int calcPow(int base, int exp) { int res = 1; while(exp-- > 0) { res *= base; } return res; }
What are the values of the following expressions: calcLog(10) : 4 calcPow(2, calcLog(10)): 16
Will the following program cause a compiler error? void print(int m) { } void print(int n) { } int main() { print(3); }
Yes
Given the following function prototype: float myfun(int a, double b); What is the return type of the function?
float
What's the output from the following program? int i = 0; int j = 0; int main() { int i = 2; int k = 3; { int j = 3; printf("i + j is %d\n", (i + j)); } k = i + j; printf("k is %d\n", k); printf("j is %d\n", j); }
i + j is 5 k is 2 j is 0
What is the output of the program? static void increment() { int i = 5; i += 5; printf("i = %d\n", i); } int main() { increment(); increment(); }
i = 10 i = 10
What is the output of the program? void increment() { static int i = 5; i += 5; printf("i = %d\n", i); } int main() { increment(); increment(); }
i = 10 i = 15
What's the output of the following code segment? #include <stdio.h> #include <string.h> #include <stdbool.h> bool isPalindrome(char *str, int size) { int i; for (i = 0; i < size; i++) { char left = str[i]; char right = str[size - 1 - i]; printf("%c %c\n", left, right); if (left != right) { return false; } } return true; } int main() { char val[] = "levVel"; printf("%s", isPalindrome(val, strlen(val)) == true ? "true" : "false"); } ###l l e e v V false ###
l l e e v V false
Which statements can complete the recursive implementation of the factorial method? int factorial(int num) { if (num == 0) return 1; else return _____________; }
num * factorial(num - 1) factorial(num - 1) * num
True or false: The return statement causes a function to terminate immediately
true
A function that does not need to return any value at all should have which return type?
void
What should be the return type of a function if there is no return value?
void
Given the macro definition: #define SQR(X) X * X
what is the value of SQR(3)? 9 and what is the value of SQR(2+3)? 11
What is the output of the following code? #include <stdio.h> void triple(double p) { p *= 3; } int main(){ double x = 1; double y = 1; triple(x); triple(y); printf("x is %d", x); printf(" y is %d\n", y); return 0; }
x is 1 y is 1
What's the value of x and y after the call of swap(x, y) as shown in the following code segment? void swap(double x, double y) { double temp = x; x = y; y = temp; } int main() { double x = 3.0; double y = 7.0; swap(x, y); }
x: 3.0 y: 7.0