Loops & Conditionals GATE CS PYQ Quiz

Last Updated :
Discuss
Comments

Question 1

Consider the following C program.
 

C
#include <stdio.h>

int main()
{
    int i, j, count;
    count = 0;
    i = 0;
    
    for (j = -3; j <= 3; j++)
    {
        if ((j >= 0) && (i++))
        {
            count = count + j;
        }
    }
    
    count = count + i;
    printf("%d", count);
    
    return 0;
}

Which one of the following options is correct?

  • The program will not compile successfully

  • The program will compile successfully and output 10 when executed

  • The program will compile successfully and output 8 when executed

  • The program will compile successfully and output 13 when executed

Question 2

Consider the following ANSI C program.

#include < stdio.h >
int main( )
{
int arr[4][5];
int i, j;
for (i=0; i<4; i++)
{
for (j=0; j<5; j++)
{
arr[i][j] = 10 * i + j;
}
}
printf("%d", *(arr[1]+9));
return 0;
}

What is the output of the above program?

  • 14

  • 20

  • 24

  • 30

Question 3

Consider the following ANSI C code segment: 

z=x + 3 + y->f1 + y->f2; 
for (i = 0; i < 200; i = i + 2) 

if (z > i) 

p = p + x + 3; 
q = q + y->f1; 
} else 

p = p + y->f2; 
q = q + x + 3; 


Assume that the variable y points to a struct (allocated on the heap) containing two fields f1 and f2, and the local variables x, y, z, p, q, and i are allotted registers. Common sub-expression elimination (CSE) optimization is applied on the code. The number of addition and the dereference operations (of the form y ->f1 or y ->f2) in the optimized code, respectively, are:
 

  • 403 and 102

  • 203 and 2

  • 303 and 102

  • 303 and 2

Question 4

What will be the output of the following C program segment?

C
char inchar = 'A';
switch (inchar)
{
case 'A' :
    printf ("choice A \n") ;
case 'B' :
    printf ("choice B ") ;
case 'C' :
case 'D' :
case 'E' :
default:
    printf ("No Choice") ;
}
  • No choice
  • Choice A
  • Choice A
    Choice B No choice
  • Program gives no output as it is erroneous

Question 5

Consider the following C function.
 

C
int fun1 (int n)
{
   int i, j, k, p, q = 0;
   for (i = 1; i < n; ++i)
   {
      p = 0;
      for (j = n; j > 1; j = j/2)
         ++p;
      for (k = 1; k < p; k = k*2)
         ++q;
   }
   return q;
}

Which one of the following is the time complexity for function fun1?
 

  • n3
     

  • n (logn)2
     

  • nlogn 
     

  • nlog(logn)
     

Question 6

Consider the following segment of C-code:

  int j, n;
j = 1;
while (j <= n)
j = j*2;

The number of comparisons made in the execution of the loop for any n > 0 is: Base of Log is 2 in all options.

  • CEIL(logn) + 2

  • n

  • CEIL(logn)

  • FLOOR(logn) + 2

Question 7

The following function computes the maximum value contained in an integer array p[] of size n (n >= 1) 
 

C
int max(int *p, int n)
{
    int a=0, b=n-1;
    while (__________)
    {
        if (p[a] <= p[b])
        {
            a = a+1;
        }
        else
        {
            b = b-1;
        }
    }
    return p[a];
}

The missing loop condition is
 

  • a != n

  • b != 0

  • b > (a + 1)

  • b != a

Question 8

The following function computes X

Y

for positive integers X and Y.

C
int exp(int X, int Y)
{
    int res = 1, a = X, b = Y;
    while ( b != 0 )
    {
        if ( b%2 == 0)
        {
            a = a*a;
            b = b/2;
        }
        else
        {
            res = res*a;
            b = b-1;
        }
    }
    return res;
}

Which one of the following conditions is TRUE before every iteration of the loop

options1
  • A

  • B

  • C

  • D

Question 9

Consider the following C function in which size is the number of elements in the array E: The value returned by the function MyX is the

C
int MyX(int *E, unsigned int size)
{
    int Y = 0;
    int Z;
    int i, j, k;
    for(i = 0; i < size; i++)
        Y = Y + E[i];
    for(i = 0; i < size; i++)
        for(j = i; j < size; j++)
        {
            Z = 0;
            for(k = i; k <= j; k++)
                Z = Z + E[k];
            if (Z > Y)
                Y = Z;
        }
    return Y;
}
  • maximum possible sum of elements in any sub-array of array E.

  • maximum element in any sub-array of array E.

  • sum of the maximum elements in all possible sub-arrays of array E

  • the sum of all the elements in the array E.

Question 10

Consider the following program fragment for reversing the digits in a given integer to obtain a new integer. Let n = D1D2…Dm

int n, rev;
rev = 0;
while (n > 0)
{
rev = rev*10 + n%10;
n = n/10;
}

The loop invariant condition at the end of the ith iteration is:

  • n = D1D2….Dm-i and rev = DmDm-1…Dm-i+1

  • n = Dm-i+1…Dm-1Dm and rev = Dm-1….D2D1

  • n != rev

  • n = D1D2….Dm and rev = DmDm-1…D2D1

There are 27 questions to complete.

Take a part in the ongoing discussion