C Quiz – 113

Question 1

C

#include <stdio.h>

int main()
{
    unsigned int i = 65000;
    while (i++ != 0);
    printf("%d", i);
    return 0;
}
Cross

Infinite Loop
 

Cross

0
 

Tick


 

Cross

Run Time Error
 



Question 1-Explanation: 

The result will be 1 but after a really long time because while loop will keep on going until i becomes 4294967295 (Assuming unsigned int is stored using 4 bytes) and as i highest limit of unsigned int is 4294967295 in next ++ operation it will become zero and we\'ll come out of loop and 1 will be printed. 

Since the time taken is long, on-line compiler may terminate the program with time limit exceeded error. If instead of unsigned int, you use unsigned short int then result (1) may come faster.
 

Question 2

Suppose you are given an implementation of a queue of integers. The operations that can be performed on the queue are: 
i. isEmpty (Q) — returns true if the queue is empty, false otherwise. 
ii. delete (Q) — deletes the element at the front of the queue and returns its value. 
iii. insert (Q, i) — inserts the integer i at the rear of the queue. 
Consider the following function: 

C

 void f (queue Q) {
int i ;
if (!isEmpty(Q)) {
   i = delete(Q);
   f(Q);
   insert(Q, i);
  }
}

What operation is performed by the above function f ?

Cross

Leaves the queue Q unchanged

Tick

Reverses the order of the elements in the queue Q

Cross

Deletes the element at the front of the queue Q and inserts it at the rear keeping the other elements in the same order

Cross

Empties the queue Q



Question 2-Explanation: 

As it is recursive call, and removing from front while inserting from end, that means last element will be deleted at last and will be inserted 1st in the new queue. And like that it will continue till first call executes insert(Q,i) function.
So, the queue will be in reverse.

Hence Option (B) is the correct answer.

Question 3
Consider the C program given below :
 #include <stdio.h>
int main ()    {
    int sum = 0, maxsum = 0,  i,  n = 6;
    int a [] = {2, -2, -1, 3, 4, 2};
    for (i = 0; i < n; i++)    {
            if (i == 0 || a [i]  < 0  || a [i] < a [i - 1])  {
                     if (sum > maxsum) maxsum = sum;
                     sum = (a [i] > 0) ? a [i] : 0;
            }
            else sum += a [i];
    }
    if (sum > maxsum) maxsum = sum ;
    printf ("%d\n", maxsum);

} 
What is the value printed out when this program is executed?
Cross
9
Cross
8
Tick
7
Cross
6


Question 3-Explanation: 
If you look for loop carefully, you will notice that it assigns sum variable to some value in if condition and increments it in the else condition. On further thought, it would be clear that this loop stores sum of increasing subsequence of positive integers in sum variable and max of sum in maxsum. Hence, maxsum - maximum sum of increasing subsequence of positive integers will get printed out when this program is executed, which is 3 + 4 = 7. This solution is contributed by Vineet Purswani //output will be 3+4 =7 {for || if 1st argument is true 2nd argument will not be calculated, and if 1st argument is false, 2nd argument will be calculated} Another Solution When i=1 -> i==0 is false, but a[i]<0 is true so condition (1) is true.Now if (sum > maxsum) is true, since sum=2 and maxsum=0.So maxsum=2. sum = (a [i] > 0) ? a [i] : 0; , sum=0 since a[i]<0.
When i=2 -> i==0 is false, a[i]<0 is true and so condition (1) is true.Now if (sum > maxsum) is false, since sum=0 and maxsum=2.Since sum = (a [i] > 0) ? a [i] : 0; , sum=0 since a[i]<0.
When i=3 -> i==0 is false , a[i]<0 is false and a [i] < a [i – 1] is false so condition (1) is false. Now sum += a [i] = 3. When i=4 -> i==0 is false , a[i]<0 is false and a [i] < a [i – 1] is false so condition (1) is false. Now sum += a [i] = 7. When i=5 -> i==0 is false , a[i]<0 is false and a [i] < a [i – 1] is true so condition (1) is true. sum > maxsum is true, since sum=7 and maxsum=2,so maxsum=7.Since sum = (a [i] > 0) ? a [i] : 0, so sum=2 since a[5]>0. This solution is contributed by nirmal Bharadwaj
Question 4
C program is given below:
# include <stdio.h>
int main ()
{
        int i, j;
        char a [2] [3] = {{'a', 'b', 'c'}, {'d', 'e', 'f'}};
        char b [3] [2];
        char *p = *b;
        for (i = 0; i < 2; i++) {
              for (j = 0; j < 3; j++) {
              *(p + 2*j + i) = a [i] [j];
              }
        }
}
 /* Add code here. Remove these lines if not writing code */ 
What should be the contents of the array b at the end of the program?
Cross
a b
c d
e f
Tick
a d
b e
c f
Cross
a c
e b
d f
Cross
a e
d c
b f


Question 4-Explanation: 
*p= a[0][0]
*(p+2) = a[0][1] *(p+4) = a[0][2] *(p+1) = a[1][0] *(p+3) = a[1][1] *(p+5) = a[1][2]
There are 4 questions to complete.


  • Last Updated : 26 Sep, 2023

Share your thoughts in the comments
Similar Reads