Functions Questions

Practice Functions MCQs with answers and explanations. Page 2 of 5.

Category
C++ Programming
Topic
Functions
Page
2 / 5
Mode
Practice

Questions

Open any question to view the answer and explanation.

In C, what does this recursive function return for the input 123, and why is the result printed twice? #include<stdio.h> int sumdig(int); int main() { int a, b; a = sumdig(123); b = sumdig(123); printf("%d, %d ", a, b); return 0; } int sumdig(int n) { int s, d; if(n!=0) { d = n%10; n = n/10; s = d+sumdig(n); } else return 0; return s; }
Open
View answer
In C, what will this program print? Note the comma operator in the function and the space in printf. #include<stdio.h> int addmult(int ii, int jj) { int kk, ll; kk = ii + jj; ll = ii * jj; return (kk, ll); } int main() { int i=3, j=4, k, l; k = addmult(i, j); l = addmult(i, j); printf("%d %d ", k, l); return 0; }
Open
View answer
In C pointer arithmetic on arrays: what is printed when the function advances a char* pointer over an initialized array? #include<stdio.h> int main() { void fun(char*); char a[100]; a[0] = 'A'; a[1] = 'B'; a[2] = 'C'; a[3] = 'D'; fun(&a[0]); return 0; } void fun(char *a) { a++; printf("%c", a); a++; printf("%c", a); }
Open
View answer
In C, function pointers and predicates: what does this program print when comparing equal integers? #include<stdio.h> int fun(int, int); typedef int (*pf) (int, int); int proc(pf, int, int); int main() { printf("%d ", proc(fun, 6, 6)); return 0; } int fun(int a, int b) { return (a==b); } int proc(pf p, int a, int b) { return ((*p)(a, b)); }
Open
View answer
In C, trace control flow with a global controlling a while loop and a recursive call to main(). What is printed? #include<stdio.h> int i; int fun(); int main() { while(i) { fun(); main(); } printf("Hello "); return 0; } int fun() { printf("Hi"); }
Open
View answer
In C, analyze recursion via calling main() from main() and a toggled local variable. What is printed? #include<stdio.h> int main() { int i=1; if(!i) printf("CuriousTab,"); else { i=0; printf("C-Program"); main(); } return 0; }
Open
View answer
In C, passing main as a function pointer: what is the program output order? #include<stdio.h> int fun(int(*)()); int main() { fun(main); printf("Hi "); return 0; } int fun(int (*p)()) { printf("Hello "); return 0; }
Open
View answer
In C function-call nesting and assignment evaluation, what will this program print? #include<stdio.h> int fun(int i) { i++; return i; } int main() { int fun(int); int i = 3; fun(i = fun(fun(i))); printf("%d ", i); return 0; }
Open
View answer
In C, evaluate nested assignments and function calls: what does this program print for k? #include<stdio.h> int func1(int); int main() { int k = 35; k = func1(k = func1(k = func1(k))); printf("k=%d ", k); return 0; } int func1(int k) { k++; return k; }
Open
View answer
On a 16-bit Turbo C (DOS) platform using compiler registers, what will this program print? #include<stdio.h> int main() { int fun(); int i; i = fun(); printf("%d ", i); return 0; } int fun() { _AX = 1990; // set return register (Turbo C extension) for int }
Open
View answer
In C, passing by address and modifying in-place: what does this program print? #include<stdio.h> void fun(int*, int*); int main() { int i = 5, j = 2; fun(&i, &j); printf("%d, %d", i, j); return 0; } void fun(int *i, int *j) { *i = *i * *i; *j = *j * *j; }
Open
View answer
Post-increment in a return and pre-decrement in printf: what is printed? #include<stdio.h> int main() { int fun(int); int i = fun(10); printf("%d ", --i); return 0; } int fun(int i) { return (i++); }
Open
View answer
Recursion with post-decremented actual argument: what does this program do? #include<stdio.h> int reverse(int); int main() { int no = 5; reverse(no); return 0; } int reverse(int no) { if (no == 0) return 0; else printf("%d,", no); reverse(no--); }
Open
View answer
Type conversions with nested calls and assignment to a float: what prints? #include<stdio.h> int fun(int); int main() { float k = 3; fun(k = fun(fun(k))); printf("%f ", k); return 0; } int fun(int i) { i++; return i; }
Open
View answer
Recursion with pre-decrement on both sides of a print: what is the output sequence? #include<stdio.h> void fun(int); typedef int (*pf)(int, int); int proc(pf, int, int); // (not used) int main() { int a = 3; fun(a); return 0; } void fun(int n) { if (n > 0) { fun(--n); printf("%d,", n); fun(--n); } }
Open
View answer
Diagnose the conditional expression and returns: what does this program do? #include<stdio.h> int check (int, int); int main() { int c; c = check(10, 20); printf("c=%d ", c); return 0; } int check(int i, int j) { int *p, *q; p = &i; q = &j; i >= 45 ? return(*p) : return(*q); }
Open
View answer
Simple conditional return in C: given i=45, what does this program print? #include<stdio.h> int check(int); int main() { int i = 45, c; c = check(i); printf("%d ", c); return 0; } int check(int ch) { if (ch >= 45) return 100; else return 10; }
Open
View answer
In C programming, what will be the actual output of this program (consider control flow and the call to exit): #include<stdio.h> #include<stdlib.h> int main() { int i = 0; i++; if (i <= 5) { printf("CuriousTab"); exit(1); main(); } return 0; } Choose the exact behavior/output.
Open
View answer
C language semantics: “In C, all functions except main() can be called recursively.” Decide whether this statement is correct or incorrect.
Open
View answer
C return semantics: “Functions cannot return more than one value at a time.” Decide whether this statement is correct or incorrect.
Open
View answer

Practice smarter

Solve a few questions daily and revisit weak topics regularly to improve accuracy.