C - Set6

01)# include <stdio.h>
int main()
{
    int x = 10;
    int y = 20;
    x += y += 10;
    printf (" %d %d", x, y);
    return 0;
}
(A) 40 20
(B) 40 30
(C) 30 30
(D) 30 40
Answer: (B)
Explanation: The main statement in question is “x += y += 10″. Since there are two += operators in the statement, associativity comes into the picture. Associativity of compound assignment operators is right to left, so the expression is evaluated as x += (y += 10).

02)#include<stdio.h>
int main()
{
    int a[10][20][30] = {0};
    a[5][2][1] = 2;
    return 0;
}
Which of the following will print the value 2 for the above code?
(A) printf(“%d”,*(((a+5)+2)+1));
(B) printf(“%d”,***((a+5)+2)+1);
(C) printf(“%d”,*(*(*(a+5)+2)+1));
(D) None of these
Answer: (C)


03)#include <stdio.h>
int main(void)
{
   int x = printf("CsTechPause");
   printf("%d", x);
   return 0;
}
(A) CsTechPause11
(B) CSTech
(C) CS
(D) CSTEchPause13

Answer: (A)
Explanation: The printf function returns the number of characters successfully printed on the screen. The string “CsTEchPause” has 11 characters, so the first printf prints CSTechPause and returns 11.

04)Predict the output of following program?
#include <stdio.h>
#define MAX 1000
int main()
{
   int MAX = 100;
   printf("%d ", MAX);
   return 0;
}
(A) 1000
(B) 100
(C) Compiler Error
(D) Garbage Value

Answer: (C)
Explanation: After preprocessing stage of compilation, the function main() changes to following
int main()
{
   int 1000 = 100;  // COMPILER ERROR: expected unqualified-id before numeric constant
   printf("%d ", 1000);
   return 0;
}

05)Assume that the size of an integer is 4 bytes, predict the output of following program.
#include <stdio.h>
int main()
{
    int i = 12;
    int j = sizeof(i++);
    printf("%d  %d", i, j);
    return 0;
}
(A) 12 4
(B) 13 4
(C) Compiler Error
(D) 0 4

Answer: (A)

06)In C, what is the meaning of following function prototype with empty parameter list
void fun()
{
   /* .... */
}
(A) Function can only be called without any parameter
(B) Function can be called with any number of parameters of any types
(C) Function can be called with any number of integer parameters.
(D) Function can be called with one integer parameter.

Answer: (B)
Explanation: Empty list in C mean that the parameter list is not specified and function can be called with any parameters. In C, to declare a function that can only be called without any parameter, we should use “void fun(void)”

07)Which of the following true about FILE *fp
(A) FILE is a keyword in C for representing files and fp is a variable of FILE type.
(B) FILE is a structure and fp is a pointer to the structure of FILE type
(C) FILE is a stream
(D) FILE is a buffered stream

Answer: (B)
Explanation: fp is a pointer of FILE type and FILE is a structure that store following information about opened file.



08)When fopen() is not able to open a file, it returns
(A) EOF
(B) NULL
(C) Runtime Error
(D) Compiler Dependent

Answer: (B)
Explanation: fopen() returns NULL if it is not able to open the given file due to any of the reasons like file not present, inappropriate permissions, etc.

09)#include <stdio.h>
int main()
{
   int x = 10;
   int y = (x++, x++, x++);
   printf("%d %d\n", x, y);
   return 0;
}
(A)13 12
(B) 13 13
(C) 10 10
(D) Compiler Dependent

Answer: (A)
Explanation: The comma operator defines a sequence point, so the option (d) is not correct.
All expressions are executed from left to right and the value of rightmost expression is returned by the comma operator.


10)What is the return type of getchar()?
(A) int
(B) char
(C) unsigned char
(D) float

Answer: (A)
Explanation: The return type of getchar() is int to accommodate EOF which indicates failure:

11)getc() returns EOF when
(A) End of files is reached
(B) When getc() fails to read a character
(C) Both of the above
(D) None of the above
Answer: (C)

12)What if the file already exists?
If a file with the same name already exists, its contents are discarded and the file is treated as a new empty file. For example, in the following program, if “test.txt” already exists, its contents are removed and “GeeksforGeeks” is written to it.
#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *fp = fopen("test.txt", "w");
    if (fp == NULL)
    {
        puts("Couldn't open file");
        exit(0);
    }
    else
    {
        fputs("CSTechPause", fp);
        puts("Done");
        fclose(fp);
    }
    return 0;
}


13)fseek() should be preferred over rewind() mainly because
(A) rewind() doesn’t work for empty files
(B) rewind() may fail for large files
(C) In rewind, there is no way to check if the operations completed successfully
(D) All of the above
Answer: (C)

14)#include <stdio.h>
int main()
{
    int i;
   
    i = 1, 2, 3;
    printf("%d", i);
   
    return 0;
}
(A) 1
(B) 3
(C) Garbage value
(D) Compile time error

Answer: (A)
Explanation: Comma acts as an operator. The assignment operator has higher precedence than comma operator. So, the expression is considered as (i = 1), 2, 3 and 1 gets assigned to variable i.


15)#include <stdio.h>
int main()
{
    int i = 1, 2, 3;
 printf("%d", i);
return 0;
}

(A) 1
(B) 3
(C) Garbage value
(D) Compile time error

Answer: (D)
Explanation: Comma acts as a separator here. The compiler creates an integer variable and initializes it with 1. The compiler fails to create integer variable 2 because 2 is not a valid identifier.

16)#include <stdio.h>
 int main()
{
    int i = (1, 2, 3);
printf("%d", i);
return 0;
}
(A) 1
(B) 3
(C) Garbage value
(D) Compile time error

Answer: (B)
 Explanation: The bracket operator has higher precedence than assignment operator. The expression within bracket operator is evaluated from left to right
but it is always the result of the last expression which gets assigned.
SHARE

About df

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment