C Quiz - 113 1

Last Updated :
Discuss
Comments

Question 1

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

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(3 * n - 1);
}
C
int f (int n) {
    if (n <= 1) return 1;
    else if (n % 2  ==  0) return f(n/2);
    else return f(3n - 1);
}
Java
int f(int n) {
    if (n <= 1) return 1;
    else if (n % 2 == 0) return f(n / 2);
    else return f(3 * n - 1);
}
Python
def f(n):
    if n <= 1:
        return 1
    elif n % 2 == 0:
        return f(n // 2)
    else:
        return f(3 * n - 1)
JavaScript
function f(n) {
    if (n <= 1) return 1;
    else if (n % 2 === 0) return f(n / 2);
    else return f(3 * n - 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?

  • (i) and (iii)

  • (i) and (iv)

  • (ii) and (iii)

  • (ii) and (iv)

Question 3

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.

C
 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?

  • Call by value : i = 70, j = 10; Call by reference : i = 60, j = 70

  • Call by value : i = 50, j = 60; Call by reference : i = 50, j = 70

  • Call by value : i = 10, j = 70; Call by reference : i = 100, j = 60

  • Call by value : i = 100, j = 60; Call by reference : i = 10, j = 70

Question 4

What is the output printed by the following C code?

C
# 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 */ 
  • dlrow

  • Null String

  • dlrld

  • worow

Question 5

Consider the C program below. What does it print?

C
# 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 */ 
  • 5, 5

  • 5, 4

  • 4, 5

  • 4, 4

Question 6

Consider the C program given below. What does it print?

C
#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 */ 
  • 2, 3

  • 2, 4

  • 3, 2

  • 3, 3

Question 7

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 ?
  • 100
  • 99
  • (100*99)/2
  • 0

Question 8

Consider the following C program:

C
#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.

  • 5

  • 4

  • 3

  • None of these

Question 9

Consider the following program:

C++
#include <iostream>
#include <cctype>
#include <stack>

#define EOF -1

std::stack<int> s;

void push(int value) { s.push(value); } /* push the argument on the stack */
int pop(void) { if (s.empty()) return EOF; int value = s.top(); s.pop(); return value; }  /* pop the top of the stack */
void flagError() { std::cerr << "Error!" << std::endl; exit(1); }

int main() {
    int c, m, n, r;
    while ((c = std::cin.get()) != EOF) {
        if (isdigit(c))
            push(c - '0');
        else if ((c == '+') || (c == '*')) {
            m = pop();
            n = pop();
            r = (c == '+') ? n + m : n * m;
            push(r);
        } else if (c != ' ')
            flagError();
    }
    std::cout << pop() << std::endl;
}
C
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define EOF -1

#define MAX_STACK_SIZE 100
int s[MAX_STACK_SIZE];
int top = -1;

void push(int value) { if (top < MAX_STACK_SIZE - 1) s[++top] = value; } /* push the argument on the stack */
int pop(void) { if (top == -1) return EOF; return s[top--]; }  /* pop the top of the stack */
void flagError() { fprintf(stderr, "Error!\n"); exit(1); }

int main() {
    int c, m, n, r;
    while ((c = getchar()) != EOF) {
        if (isdigit(c))
            push(c - '0');
        else if ((c == '+') || (c == '*')) {
            m = pop();
            n = pop();
            r = (c == '+') ? n + m : n * m;
            push(r);
        } else if (c != ' ')
            flagError();
    }
    printf("%d\n", pop());
}
Java
import java.util.*;

public class Main {
    static Stack<Integer> stack = new Stack<>();

    static void push(int value) { stack.push(value); } /* push the argument on the stack */
    static int pop() { return stack.isEmpty() ? -1 : stack.pop(); }  /* pop the top of the stack */
    static void flagError() { System.err.println("Error!"); System.exit(1); }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int c, m, n, r;
        while ((c = scanner.next().charAt(0)) != -1) {
            if (Character.isDigit(c))
                push(c - '0');
            else if (c == '+' || c == '*') {
                m = pop();
                n = pop();
                r = (c == '+') ? n + m : n * m;
                push(r);
            } else if (c != ' ')
                flagError();
        }
        System.out.println(pop());
    }
}
Python
class Stack:
    def __init__(self):
        self.items = []
    def push(self, item):
        self.items.append(item)  # push the argument on the stack
    def pop(self):
        return self.items.pop() if self.items else -1  # pop the top of the stack

def flagError():
    print("Error!")
    exit(1)

s = Stack()

while True:
    try:
        c = input()
        if c == '':
            break
        c = ord(c)
        if c >= ord('0') and c <= ord('9'):
            s.push(c - ord('0'))
        elif c == ord('+') or c == ord('*'):
            m = s.pop()
            n = s.pop()
            r = n + m if c == ord('+') else n * m
            s.push(r)
        elif c != ord(' '):
            flagError()
    except EOFError:
        break
print(s.pop())
JavaScript
class Stack {
    constructor() {
        this.items = [];
    }
    push(item) { this.items.push(item); } /* push the argument on the stack */
    pop() { return this.items.length === 0 ? -1 : this.items.pop(); }  /* pop the top of the stack */
}

function flagError() { console.error('Error!'); process.exit(1); }

const stack = new Stack();

process.stdin.on('data', function (data) {
    const input = data.toString();
    for (let c of input) {
        if (!isNaN(c)) {
            stack.push(parseInt(c));
        } else if (c === '+' || c === '*') {
            const m = stack.pop();
            const n = stack.pop();
            const r = (c === '+') ? n + m : n * m;
            stack.push(r);
        } else if (c !== ' ') {
            flagError();
        }
    }
});

process.stdin.on('end', function () {
    console.log(stack.pop());
});

What is the output of the program for the following input ? 5 2 * 3 3 2 + * +

  • 15

  • 25

  • 30

  • 150

Question 10

Consider the following C program.

C
#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:

  • 0, c

  • 0, a+2

  • '0', 'a+2'

  • '0', 'c'

Tags:

There are 10 questions to complete.

Take a part in the ongoing discussion