C Expressions and Control Flow questions

Q53

GATE CS 2017 CS-1C ProgrammingNAT2 marks
Consider the following C program.
#include <stdio.h>
#include <string.h>

void printlength(char *s, char *t) {
    unsigned int c = 0;
    int len = ((strlen(s) - strlen(t)) > c) ? strlen(s): strlen(t);
    printf("%d\n", len);
}

void main() {
    char *x = "abc";
    char *y = "defgh";
    printlength(x,y);
}
Recall that strlen is defined in string.h as returning a value of type size_t, which is an unsigned int. The output of the program is ____________.
Show answer without attempting

Answer: 3.0

Q55

GATE CS 2017 CS-1C ProgrammingNAT2 marks
The output of executing the following C program is ____________.
#include <stdio.h>

int total(int v) {
    static int count = 0;
    while(v) {
        count += v&1;
        v >>= 1;
    }
    return count;
}

void main() {
    static int x = 0;
    int i = 5;
    for(; i > 0; i--) {
        x = x + total(i);
    }
    printf("%d\n", x);
}
Show answer without attempting

Answer: 23.0

Q37

GATE CS 2017 CS-2C ProgrammingMCQ2 marks−2/3 if wrong
Consider the C program fragment below which is meant to divide xx by yy using repeated subtractions. The variables x,y,qx, y, q and rr are all unsigned int.
while (r >= y) {
    r = r - y;
    q = q + 1;
}
Which of the following conditions on the variables x,y,qx, y, q and rr before the execution of the fragment will ensure that the loop terminates in a state satisfying the condition x == (y*q + r)?
Show answer without attempting

Answer: C

Q54

GATE CS 2017 CS-2C ProgrammingNAT2 marks
Consider the following C Program.
#include <stdio.h>
int main() {
    int m = 10;
    int n, n1;
    n = ++m;
    n1 = m++;
    n--;
    --n1;
    n -= n1;
    printf("%d", n);
    return 0;
}
The output of the program is ____________.
Show answer without attempting

Answer: 0.0

0/40 correct0 marks

Related topics in C Programming