C Quiz – 113

Question 1
Output of following program under the assumption that numbers are stored in 2's complement form.
#include<stdio.h>
int main()
{
   printf("%c\n", ~('C' * -1));
   return 0;
}
Contributed by Sowmya.L.R
Tick
B
Cross
A
Cross
Compiler Error
Cross
C


Question 1-Explanation: 
executed without any error or warning messages and the output for the above code is \'B\' The above program processes as below Step 1: First (\'C\' *-1) is processed ASCII value of \'C\' is 67 and it is multiplied with -1 as 67 * (-1) = -67 Step 2: The binary representation of -67 is 10111101 The bitwise negation of 10111101 becomes (01000010 ) 2 = (66) 10 Step 3: 66 is the ASCII value of \'B\' So ~(\'C\'*-1) = 66 and so the output of the above the program is B
Question 2

The function f is defined as follows: 

C

int f (int n) {
    if (n <= 1) return 1;
    else if (n % 2  ==  0) return f(n/2);
    else return f(3n - 1);
}

Assuming that arbitrarily large integers can be passed as a parameter to the function, consider the following statements.
1. The function f terminates for finitely many different values of n ≥ 1. 
ii. The function f terminates for infinitely many different values of n ≥ 1. 
iii. The function f does not terminate for finitely many different values of n ≥ 1. 
iv. The function f does not terminate for infinitely many different values of n ≥ 1. 
Which one of the following options is true of the above?

Cross

(i) and (iii)

Cross

(i) and (iv)

Cross

(ii) and (iii)

Tick

(ii) and (iv)



Question 2-Explanation: 

The function terminates for all values having a factor of 2 {(2.x)2==0}
So, (i) is false and (ii) is TRUE.
Let n = 3, it will terminate in 2nd iteration.
Let n=5, it will go like 5 - 14 - 7 - 20 - 10 - 5 – and now it will repeat.
And any number with a factor of 5 and 2, there are infinite recursions possible.
So, (iv) is TRUE and (iii) is false.

Hence Option(D) is the correct Option.

Question 3
Consider the following C program:
   #include 
           #define EOF -1
           void push (int); /* push the argument on the stack */
           int pop  (void); /* pop the top of the stack */
           void flagError ();
           int main ()
          {         int c, m, n, r;
                     while ((c = getchar ()) != EOF)
                    { if  (isdigit (c) )
                               push (c);
                     else if ((c == '+') || (c == '*'))
                    {          m = pop ();
                                n = pop ();
                                r = (c == '+') ? n + m : n*m;
                                push (r);
                      }
                      else if (c != ' ')
                               flagError ();
             }
              printf("% c", pop ());
}
What is the output of the program for the following input ? 5 2 * 3 3 2 + * +
Cross
15
Tick
25
Cross
30
Cross
150


Question 3-Explanation: 
  The function of the program is:- 1) If the current character is a digit it pushes into stack 2) Else if the current character is operator,  it pops two elements and then performs the operation. Finally it pushes the resultant element into stack. Initially stack s is empty. 5 2 * 3 3 2 + * + 1) 5 -> It pushes into s 2) 2 -> It pushes into s 3) * -> It pops two elements n = 2, m=5 n*m = 10 It pushes 10 into s 4) 3 -> It pushes into s 5) 3 -> It pushes into s 6) 2 -> It pushes into s 7) + -> n=2, m=3 n+m=5 It pushes 5 into s 8) * -> n=5, m=3 n*m=15 It pushes 15 into s 9) + -> n=15, m=10 n+m = 25 It pushes 25 into s.   Finally the result value is the only element present in stack. This solution is contributed  by Anil Saikrishna Devarasetty. Result = 25
Question 4
Consider the program below in a hypothetical language which allows global variable and a choice of call by reference or call by value methods of parameter passing.
 int i ;
program main ()
{
    int j = 60;
    i = 50;
    call f (i, j);
    print i, j;
}
procedure f (x, y)
{           
    i = 100;
    x = 10;
    y = y + i ;
}
Which one of the following options represents the correct output of the program for the two parameter passing mechanisms?
Cross
Call by value : i = 70, j = 10; Call by reference : i = 60, j = 70
Cross
Call by value : i = 50, j = 60; Call by reference : i = 50, j = 70
Cross
Call by value : i = 10, j = 70; Call by reference : i = 100, j = 60
Tick
Call by value : i = 100, j = 60; Call by reference : i = 10, j = 70


Question 4-Explanation: 
Call by value: A copy of parameters will be passed and whatever updations are performed will be valid only for that copy, leaving original values intact.
Call by reference: A link to original variables will be passed, by allowing the function to manipulate the original variables.
Question 5
What is the output printed by the following C code?
# include <stdio.h>
int main ()
{
    char a [6] = "world";
    int i, j;
    for (i = 0, j = 5; i < j; a [i++] = a [j--]);
    printf ("%s\n", a);
}
 /* Add code here. Remove these lines if not writing code */ 
Cross
dlrow
Tick
Null String
Cross
dlrld
Cross
worow


Question 5-Explanation: 
As at the base address or starting of the string \"Null\" is placed, so while reading array if Null comes it assumes that this is the end of array, so it terminates here only.
Question 6
Consider the C program below. What does it print?
# include <stdio.h>
# define swapl (a, b) tmp = a; a = b; b = tmp
void swap2 ( int a, int b)
{
        int tmp;
        tmp = a; a = b; b = tmp;
 }
void swap3 (int*a, int*b)
{
        int tmp;
        tmp = *a; *a = *b; *b = tmp;
}
int main ()
{
        int num1 = 5, num2 = 4, tmp;
        if (num1 < num2) {swap1 (num1, num2);}
        if (num1 < num2) {swap2 (num1 + 1, num2);}
        if (num1 >= num2) {swap3 (&num1, &num2);}
        printf ("%d, %d", num1, num2);
}
 /* Add code here. Remove these lines if not writing code */ 
Cross
5, 5
Cross
5, 4
Tick
4, 5
Cross
4, 4


Question 6-Explanation: 
\"if (num1 > = num2) {swap3 (&num1, &num2);}\" statement is true, so call by reference will be performed.
Question 7
Consider the C program given below. What does it print?

#include <stdio.h>
int main ()
{
        int i, j;
        int a [8] = {1, 2, 3, 4, 5, 6, 7, 8};
        for(i = 0; i < 3; i++) {
             a[i] = a[i] + 1;
             i++;
        }
        i--;
        for (j = 7; j > 4; j--) {
              int i = j/2;
              a[i] = a[i] - 1;
        }
        printf ("%d, %d", i, a[i]);
}
 /* Add code here. Remove these lines if not writing code */ 
Cross
2, 3
Cross
2, 4
Tick
3, 2
Cross
3, 3


Question 7-Explanation: 
Be careful about the scope of i,
there are two variables named: i, with different scope. There are 2 main points to consider while solving this question. Scope of variable i and integer division. First for loop will run for i = 0, 2 and 4 as i is incremented twice inside loop and resultant array will be a  = 2, 2, 4, 4, 5, 6, 7, 8  (Loop will terminate at i = 4) After that i value is 3 as there is a decrement operation after for loop. Next for loop is running for j = 7, 6 and 5 and corresponding i values which is a local variable inside for loop will be 3 (7/2), 3 (6/2) and 2 (5/2). Array after this for loop will be a  = 2, 2, 3, 2, 5, 6, 7, 8 After the for loop, current i value is 3 and element at a[3] = 2. This solution is contributed by Pranjul Ahuja.
Question 8
Consider the following C code:
 int A[100][100];
 int main()
 {
    for(int i=1; i < 100 ; i++)
        for(int j=1; j < 100;j++)
            A[i][j] = (i/j)*(j/i);
   return 0;
 }

What will be the sum of the all the elements of double dimensional array A after implementing the above function ?
Cross
100
Tick
99
Cross
(100*99)/2
Cross
0


Question 8-Explanation: 
Since array is global A[0][j] = A[i][0] = 0.
Final value after adding all the values of this array will be 99 . Only diagonal element will be having 1’s except A[0][0] and rest all are zeroes as integer division (i/j) when j > i will be 0.
Question 9
Consider the following C program:

#include <stdio.h>

int counter = 0;

int calc(int a, int b) {
  int c;

  counter++;
  if (b == 3)
    return (a * a * a);
  else {
    c = calc(a, b / 3);
    return (c * c * c);
  }
}

int main() {
  calc(4, 81);
  printf("%d", counter);
}
The output of this program is ________ . Note - This was Numerical Type question.
Cross
5
Tick
4
Cross
3
Cross
None of these


Question 9-Explanation: 
\"1\"
Question 10
Consider the following C program.

#include <stdio.h>
struct Ournode {
  char x, y, z;
};

int main() {
  struct Ournode p = {'1', '0', 'a' + 2};
  struct Ournode *q = &p;
  printf("%c, %c", *((char *)q + 1), *((char *)q + 2));
  return 0;
}
The output of this program is:
Tick
0, c
Cross
0, a+2
Cross
\'0\', \'a+2\'
Cross
\'0\', \'c\'


Question 10-Explanation: 
\'a\' + 2 will be \'c\', so Ournode p = {\'1\', \'0\', \'c\'} and output will be 0, c. See: storage for Strings in C, string. Option (A) is correct.
There are 10 questions to complete.


  • Last Updated : 26 Sep, 2023

Share your thoughts in the comments
Similar Reads