Vous êtes sur la page 1sur 92

next →← prev

C Fundamental Test 1

1) Which of the following is the first operating system developed


using C programming language?
a. Windows
b. DOS
c. Mac
d. UNIX

Hide Answer

The correct option is (d).

Explanation:

C programming language is invented for developing an operating system called UNIX. By


1973, the complete UNIX OS is developed using C.

2) The C compiler used for UNIX operating system is


a. cc
b. gcc
c. vc++
d. Borland

Hide Answer

The correct option is (a).

Explanation:

Compiler used for UNIX is 'cc' their full form is C compiler. gcc is compiler for linux.
Borland and vc++ is compiler for windows.
3) Which of the following is a logical AND operator?
a. ||
b. !
c. &&
d. None of the above

Hide Answer

The correct option is (c).

Explanation:

The && is called logical AND operator. If both operands are non-zero, then the condition
becomes true.

The || is called logical OR operator. If any of the two operands are non-zero, then the
condition becomes true.

The ! is called logical NOT operator. It is used for reversing the logic state of its operand.

4) Which format specifier is used for printing double value?


a. %Lf
b. %L
c. %lf
d. None of the above

Hide Answer

The correct option is (c).

Explanation:

The %lf format specifier is used for printing the double value in a C program.

5) Which of the following statement is used to free the allocated


memory space for a program?
a. vanish(var-name);
b. remove(var-name);
c. erase(var-name);
d. free(var-name);

Hide Answer

The correct option is (d).

Explanation:

The memory allocated by malloc(), calloc(), or realloc() function is deallocated by using the
library function free(var-name).

6) In a library of C programming language, which of the following


header file is used for performing mathematical operations?
a. conio.h
b. dos.h
c. math.h
d. stdio.h

Hide Answer

The correct option is (c).

Explanation:

"math.h" is a header file used for performing mathematical operation in a library of C


programming language.

7) What are the various types of real data type in C language?


a. long double, short int
b. float, long double
c. short int, double, long int, float
d. float, double, long double
Hide Answer

The correct option is (d).

Explanation:

Floating data type is known as real data type.

There are three types of floating data type:

o float with storage size of 4 byte


o long double with storage size of 10 byte
o double with storage size of 8 byte

8) The statement used for printing \n on the screen is:


a. printf("");
b. printf('\n');
c. printf("\\n");
d. printf("n\");

Hide Answer

The correct option is (c).

Explanation:

In C language,"\n" is the escape sequence for printing a new line character. For a statement
printf("\\n"); statement , "\\" symbol will be printed as "\" and "n" is known as a common
symbol.

9) Which of the following are declarations?


1. float square (float x){?}
2. double pow(double, double);
3. extern int x;

Options are given below:

a. 1
b. 1 and 3
c. 2
d. 1 and 2
Hide Answer

The correct option is (b).

Explanation:

double pow(double, double); instruction is a function prototype declaration

extern int x; instruction is an external variable declaration.

Therefore 1 and 3 are declarations and 2 is definition.

10) Which header file is used to define input/output functions,


macros and prototypes?
a. memory.h
b. math.h
c. dos.h
d. stdio.h

Hide Answer

The correct option is (d).

Explanation:

Header file stdio.h is used for defining the macros, variable and various functions for
performing input and output operation in C-language.

11) Single line comment in C language begins with _______


a. :
b. //
c. */
d. /*

Hide Answer

The correct option is (b).


Explanation:

For giving the comment in single line two immediate forward slashes are used. For a multi
line comment it begins with /* and should be terminated with */.

12) What is the data type of "PI" for the below statement:
1. #define PI 3.141
a. Float data type
b. Double datatype
c. There is no data type associated with PI
d. Syntax error, semi colon is missing with definition of PI

Hide Answer

The correct option is (c).

Explanation:

Text associated with macro statement gets expanded at line of call. The expanded text is by
default a constant with no data type associated with PI.

13) Which operator can be used for accessing the value stored at
address of a pointer variable?
a. #
b. *
c. &&
d. @

Hide Answer

The correct option is (b).

Explanation:

The pointer operator is,

& (address operator) = It gives address of the variable

*(Value operator) = It gives value stored at particular address

14) The types of linkages in C programming language are:


a. External linkage and None linkage
b. Internal linkage and None linkage
c. Internal linkage, External linkage and None linkage
d. Internal linkage and External linkage

Hide Answer

The correct option is (c).

Explanation:

o Internal linkage: A functions and static variables with file scope.


o External linkage: A global, functions and non-static variables.
o None linkage: A local variables.

15) Which of the following is true about C Programming?


a. Platform Independent
b. High level language
c. Machine Independent
d. Assembly language

Hide Answer

The correct option is (c).

Explanation:

C-programming language is machine independent programming language. It provides the


feature of portability of code means source code written on one machine can be run on any
other machines.

16) What is the correct value returned to the operating system


upon successful completion of a program?
a. 0
b. -1
c. 1
d. Programs do not return a value
Hide Answer

The correct option is (a).

Explanation:

After successful completion of a program, 0 is returned to the operating system.

17) Who is known as the founder of C language?


a. James Gosling
b. Martin Richard
c. Brian Kernighan
d. Dennis Ritchie

Show Answer

18) The C variables are case insensitive.


a. True
b. False

Hide Answer

The correct option is (b).

Explanation:

The C variables are case sensitive. This means that variables sal, Sal and SAL would be
treated as different variables in C.

19) A character variable can store ___ character(s) at a time.


a. 1
b. 2
c. 0
d. NULL

Hide Answer

The correct option is (a).

Explanation:
A character variable can at a time store only one character. In fact, if we execute the
following statements, what gets stored in variable ch is not really the character constant,
but the ASCII value of 'A', which is 65.

1. char ch;
2. ch= 'A';

20) How would you round off a value from 1.66 to 2.0?
a. floor(1.66)
b. ceil(1.66)
c. roundup(1.66)
d. roundto(1.66)

Hide Answer

The correct option is (b).

Explanation:

The ceil(1.66) is used for round off a value from 1.66 to 2.0. The ceil() returns upper value
of a fractional part and floor() returns lower value.

1. /* Example for floor() and ceil() functions:*/


2. #include<stdio.h>
3. #include<math.h>
4. int main()
5. {
6. printf("\n Result : %f" , ceil(1.44) );
7. printf("\n Result : %f" , ceil(1.66) );
8. printf("\n Result : %f" , floor(1.44) );
9. printf("\n Result : %f" , floor(1.66) );
10. return 0;
11. }
12. // Output:
13. //Result : 2.000
14. //Result : 2.000
15. //Result : 1.000
16. //Result : 1.000

next →← prev
C Control Statements Test 1
C control statements test paper contains questions from decision statement: if-else and
switch, loop statement: for loop, while loop & do-while loop and jump statement: break
and continue.

1) Which data type cannot be checked in switch-case


statement?
a. enum
b. character
c. integer
d. float

Hide Answer

The correct option is (d).

Explanation:

In C-languageswitch/case statement is defined by the language specification to use


an int value therefore we cannot use a float value in switch/case statement.

2) How many times "javaTpoint" is printed?


1. #include<stdio.h>
2. int main()
3. {
4. int x;
5. for(x=-1; x<=10; x++)
6. {
7. if(x < 5)
8. continue;
9. else
10. break;
11. printf("javaTpoint");
12. }
13. return 0;
14. }
a. 10 times
b. 11 times
c. 0 times
d. Infinite times

Hide Answer

The correct option is (c).

Explanation:

In program the x is initialized with -1. As x < 5 (since x is -1) it will start with continue
statement.

Continue means "stop the current iteration and proceed to the next iteration". Therefore
x becomes 0 now. This will take place until x becomes 5.

Now if the value of x=5, it will enter the else part where it encounters the break
statement, as a result it will come out of for loop. Hence it will not go to printf statement.

Therefore javaTpoint will be printed 0 times.

3) How many times while loop is executed if a short int is 2 byte


wide?
1. #include<stdio.h>
2. int main()
3. {
4. int i=1;
5. while(i <= 155)
6. {
7. printf("%c %d\n", i, i);
8. i++;
9. }
10. return 0;
11. }
a. 154 times
b. 155 times
c. 156 times
d. Infinite times

Hide Answer

The correct option is (b).

Explanation:

The size of short int which is 2 byte wide does not affect the while() loop operation.

Therefore thewhile (i <= 155) loop will executed 155 times.

4) Which statement is correct about the below program?


1. #include<stdio.h>
2. int main()
3. {
4. int i = 8, j = 24;
5. if(i = 8) && if(j = 24)
6. printf("Welcome Programmer");
7. return 0;
8. }
a. Welcome Programmer
b. Error: Undeclared identifier if
c. Error: Expression syntax
d. No output

Hide Answer

The correct option is (c).

Explanation:

In program 5th line i.e. if(i = 8) && if(j = 24) the "Expression syntax" error occur.

Hence the statement should be like if((i == 5) && (j == 10)).

Therefore on compiling the program Error: Expression syntax is occurring.


5) Find out the error, if any in the below program?
1. #include<stdio.h>
2. int main()
3. {
4. int j = 1;
5. switch(j)
6. {
7. printf("Hello programmer!");
8. case 1:
9. printf("Case1");
10. break;
11. case 2:
12. printf("Case2");
13. break;
14. }
15. return 0;
16. }
a. No error in program and prints "Case1"
b. Error: Invalid printf statement after switch statement
c. Error: No default specified
d. None of the above

Hide Answer

The correct option is (a).

Explanation:

In program switch statement is used for switch(j) it becomes switch(1) because i is


initialized with 1.

Therefore the case 1: block is get executed. Hence it prints "Case1".

Printf ("Hello programmer!"); is ignored by the compiler.

Hence there is no error in program and prints "Case1".

6) Find out the error, if any in the while loop of below program.
1. #include<stdio.h>
2. int main()
3. {
4. int j=1;
5. while()
6. {
7. printf("%d\n", j++);
8. if(j>5)
9. break;
10. }
11. return 0;
12. }
a. There should be a semicolon in the while statement
b. The while loop should be replaced with do-while loop
c. There should be a condition in the while loop
d. No error

Hide Answer

The correct option is (c).

Explanation:

In program "Expression syntax" error occur because the while() loop must have conditional
expression.

For Example: while (j >5) { ... }

Therefore for removing the ?Expression syntax? error there should be a condition in the
while loop.

7) If scanf() statement is used for storing a value in char variable,


then along with the value a carriage return (\r) also gets stored.
a. True
b. False

Hide Answer

The correct option is (b).

Explanation:
No, if scanf() statement is used then the carriage return tells the compiler to read the input
from buffer after the ENTER key is pressed by the user. Therefore the value of carriage
return (\r) is not get stored in memory.

8) Find out whether both the loops in a program prints the correct
string length?
1. #include<stdio.h>
2. main()
3. {
4. int j;
5. char s[] = "javaTpoint";
6.
7. for(j=0; s[j]; ++j);
8. printf("%d \n", j);
9.
10. j=0;
11. while(s[j++]);
12. printf("%d ", j);
13. }
a. Yes, both the loops prints the correct string length
b. Only while loop prints the correct string length
c. Only for loop prints the correct string length
d. Compile error in the program

Hide Answer

The correct option is (c).

Explanation:

1. Output: 10
2. 11

In while loop the incorrect string length is printed because while loop variable 'i' get
incremented after checking for '\0', hence giving 1 more than the length of string.

Therefore only for loop prints the correct string length.

9) The break statement is used to take control out


of switch and continue statement is used to take control of the
beginning of the switch?
a. Yes
b. No

Hide Answer

The correct option is (b).

Explanation:

No, because continue statement can work only with loops in C-programming and not
with switch.

10) For printing the value of a and b given below, which printf()
statement will you use?
1. #include<stdio.h>
2. main()
3. float a=3.14;
4. double b=3.14;
a. printf("%Lf %f", a, b);
b. printf("%Lf %Lf", a, b);
c. printf("%f %Lf", a, b);
d. printf("%f %lf", a, b);

Hide Answer

The correct option is (d).

Explanation:

For printing the double value %lf is used as format specifier.

For printing the float value %f is used as format specifier.

Therefore for printing the value of a and b the syntax of printf statement is printf("%f
%lf", a, b);

11) Which statements are correct about an if-else statement in a


C-program?
1. Nested if-else statements are allowed
2. Every if-else statement can be replaced by an equivalent statement using ?:
operators
3. Multiple statement in else block are allowed
4. Multiple statement in if block are allowed

a. 1, 3 and 4
b. 1, 2, 3 and 4
c. 2 , 3and 4
d. 1 and 4

Hide Answer

The correct option is (a).

Explanation:

Nested if-else statement is allowed in C-program we can use if-else statement within if or
else statement.

Multiple statements in if or else block are allowed because we can execute multiple
statements against true value of if or else condition by placing the statements within { ?.. }.

Mostly if-else statement can be replaced by ternary operator but there are some exceptions
also in which if-else statement cannot be replaced by ternary operator.

Therefore 1, 3 and 4 statements are correct about if-else statement.

12) Find out the error, if any in the below program?


1. #include<stdio.h>
2. int main()
3. {
4. int P = 10;
5. switch(P)
6. {
7. case 10:
8. printf("Case 1");
9.
10. case 20:
11. printf("Case 2");
12. break;
13.
14. case P:
15. printf("Case 2");
16. break;
17. }
18. return 0;
19. }
a. Error: Constant expression required at line case P:
b. Error: There is no break statement in each case
c. Error: No default value is specified
d. No error

Hide Answer

The correct option is (a).

Explanation:

On compiling the program compiler will report an error "constant expression required" in the
line case P: because variable name is not allowed to be used with case statements.

The case statements only accept constant expression. Therefore the Error: Constant
expression required at line case P: is occur.

13) Find out the error, if any in the below program?


1. #include<stdio.h>
2. int main()
3. {
4. int i = 1;
5. switch(i)
6. {
7. case 1:
8. printf("Case1");
9. break;
10. case 1*2+2:
11. printf("Case2");
12. break;
13. }
14. return 0;
15. }
a. Error: in switch statement
b. Error: in case 1*2+4 statement
c. Error: No default specified
d. No Error
Hide Answer

The correct option is (d).

Explanation:

In switch statement constant expression are allowed therefore in case 1*2+4 statement it
will give no error.

Therefore it prints "Case1" in the output of program.

14) A long integer is at least 32 bits wide and a short integer is


at least 16 bits wide
a. True
b. False

Hide Answer

The correct option is (a).

Explanation:

The basic C compiler used is 16 bit compiler, below are the size of their data types:

The size of long int is 4 bytes wide i.e. 32 bits.

The size of short int is 2 bytes wide i.e. 16 bits

15) A char variable can store either a Unicode character or an


ASCII character.
a. True
b. False

Hide Answer

The correct option is (a).

Explanation:

Yes, char variable is allowed to store either a Unicode character or an ASCII character
because encoding of character data type is done in Unicode or ASCII format.
16) Which of the following statements are correct about below C-
program?
1. #include<stdio.h>
2. int main()
3. {
4. int x = 100, y = 100%80, j;
5. for(j=1; j<10; j++)
6. if(x != y);
7. printf("x = %d y = %d\n", x, y);
8. return 0;
9. }
1. The program produce the output x=100 y=20
2. The printf() function run for 10 times
3. The semi colon(;) after the if(x!=y) will not produce any error
4. The program will produce no output

The options are given below:

a. 2
b. 1,3
c. 3,4
d. 4

Hide Answer

The correct option is (b).

Explanation:

The statement 1 is true because x=100 and y=20 is the output of a program.

The statement 2 is false because printf() function is not inside for loop. Therefore printf
statement only runs for 1 time.

The statement 3 is true because the semicolon is used for terminating a conditional
statement. Therefore if(x!=y); is allowed in C.

The statement 4 is false because the program is producing output x=100 and y=20.

Therefore only statement 1 and 3 are correct statement.


17) Which of the following statements are correct about for loop in
C-program?
1. All things that can be done using a for loop can also be done using a while loop.
2. for loop can be used if we want statements in a loop get executed at least once.
3. for loop works faster than a while loop.
4. for(;;); implements an infinite loop.

The options are given below:

a. 1
b. 1, 2, 3
c. 2, 3, 4
d. 1, 2, 4

Hide Answer

The correct option is (d).

Explanation:

For loop is used if we want statements in loop get executed at least once. Therefore for loop
works slower than a while loop i.e. statement 3 is incorrect.

Remaining 3 statements about for loop is correct.

Therefore statement 1, 2, 4 are the correct statements.

18) What is the output of the given program, if a short int is 2


bytes wide?
1. #include<stdio.h>
2. int main()
3. {
4. short int i = 0;
5. for(i<=5 && i>=-1; ++i; i>0)
6. printf("%u,", i);
7. return 0;
8. }
a. Expression syntax error
b. 1 .... 65535
c. 0, 1, 2, 3, 4, 5
d. No output

Hide Answer

The correct option is (b).

Explanation:

In for loop expression i.e. for(i<=5 && i>=-1; ++i; i>0) the expression i<=5 && i>=-1
is the loop condition. Expression ++i increment the expression.

In given for loop condition the loop start from 1and it gets executed till the limit of integer
i.e. 65535.

Therefore the output of the program is 1 ... 65535.

19) Can we use switch statement to switch on strings in C?


a. Yes
b. No

Hide Answer

The correct option is (b).

Explanation:

In switch statemen,t the cases must be either constant expression or an integer constant.

Therefore it is not allowed to use switch statement to switch on strings in C programming.

20) What is the output of the given program?


1. #include<stdio.h>
2. int main()
3. {
4. int a=5;
5. do
6. {
7. printf("%d\n",a);
8. a= -1;
9. }while (a>0);
10. return 0;
11. }
a. -1
b. 5
c. 0
d. Compile error

Hide Answer

The correct option is (b).

Explanation:

A do-while allows body of the loop to get executed before testing the condition. Therefore to
begin with, value of a, i.e. 5, gets printed, and then the control reaches the statement a=-
1.

Since -1 is not greater than 0, so the condition fails and the loop is terminated at the value
of a=5.

Therefore the output of program is 5.

C Functions Test 1

1) What is the built-in library function for comparing the two


strings?
a. strcmp()
b. equals()
c. str_compare()
d. string_cmp()

Hide Answer

The correct option is (a).

Explanation:

The strcmp() is a built-in function available in "string.h" header file. It is used for comparing
the two strings. It returns 0 if both are same strings. It returns positive value greater than 0
if first string is greater than second string, otherwise it returns negative value.
2) What is passed when we pass an array as a function
argument?
a. Base address of an array
b. Address of the last element of array
c. First value of elements in array
d. All value of element in array

Hide Answer

The correct option is (a).

Explanation:

On passing the name of array as function argument; the name contain the base address of
an array and base address is updated inside a main function.

3) Which function finds the first occurrence of a substring in


another string?
a. strchr()
b. strnset()
c. strstr()
d. None of these.

Hide Answer

The correct option is (c).

Explanation:

The first occurrence of a substring in another string is founds in strstr() function.

4) What is the built-in library function for adjusting the allocated


dynamic memory size.
a. calloc
b. malloc
c. realloc
d. resize

Hide Answer
The correct option is (c).

Explanation:

realloc() is a built in library function for adjusting the dynamic memory size. malloc() and
calloc() allocates the memory but don't resize. There is no built in function with a name
resize().

5) Which keyword is used to transfer control from a function back


to the calling function?
a. return
b. go back
c. switch
d. goto

Hide Answer

The correct option is (a).

Explanation:

In C language, the return function stops the execution of function and returns a value to
calling function. Execution is begins in a calling function by instantly following the call.

6) Which library function can change an unsigned long integer to


a string?
a. system()
b. ltoa()
c. ultoa()
d. unsigned long can't be change into a string

Hide Answer

The correct option is (c).

Explanation:

The function ultoa() is used for converting an unsigned long integer to a string.
7) What is the output of below C program?
1.
2. #include<stdio.h>
3. int function1(int);
4.
5. int main()
6. {
7. int k=30;
8. k = function1(k=function1(k=function1(k)));
9. printf("k=%d\n", k);
10. return 0;
11. }
12. int function1(int k)
13. {
14. k++;
15. return k;
16. }
a. k=30
b. k=31
c. k=32
d. k=33

Hide Answer

The correct option is (d).

Explanation:

Step 1: int k=30; The variable k is declared as an integer type and initialized to 30.

Step 2: k=function1(k= function1(k=function1(k))); The function1 (k) increment


the value of k by 1 and return it. In program function1(k) is called 3 times. Hence the value
of k increments from k=30 to 33. Therefore result stored in the variable k=33.

Step 3: printf("k=%d\n", k); It prints the value of variable k =33.

8) What is the purpose of using fflush() function?


a. Flushes only specified stream.
b. Flushes file buffer.
c. Flushes input/output buffer.
d. Flushes all streams and specified buffer.

Hide Answer

The correct option is (d).

Explanation:

Using "flush()" function we can flush any buffered output associated with a filename, which
is either a shell command for redirecting output or a file opened for writing.

For example:

1.
2. fflush (FilePointer);
3. fflush (NULL); //It flushes all streams

9) What is the value returned by strcmp() function when two


strings are the equal?
a. 2
b. 1
c. 0
d. Error

Hide Answer

The correct option is (c).

Explanation:

C library function strcmp() compares the two strings with each other and the value is return
accordingly.

1.
2. int strcmp (const char *str1, const char *str2)

Comparison occurs between a first string (str1) with a second string (str2).

On comparing the two string, the values return by a function strcmp() are:

o If, str1 is equal to str2 then Return value = 0


o If, str1 is greater than str2 then Return value > 0
o If, str1 is less than str2 then Return value < 0
10) Which function disconnects the stream from a file pointer?
a. fclose()
b. fremove()
c. remove()
d. file pointer set to NULL

Hide Answer

The correct option is (a).

Explanation:

Function that disconnects the stream from a file pointer is fclose(), it flushes the buffers
associated with a stream and disconnects the stream from a file pointer.

11) If the integer data type (int) is 2 bytes wide, what is the output
of below program?
1. #include <stdio.h>
2. void fun(char**);
3.
4. int main()
5. {
6. char *argmntv[] = {"gh", "ef", "cd", "ab"};
7. fun(argmntv);
8. return 0;
9. }
10. void fun(char **x)
11. {
12. char *y;
13. y= (x+= sizeof(int))[-1];
14. printf("%s\n", y);
15. }
a. gh
b. ab
c. ef
d. cd
Hide Answer

The correct option is (c).

Explanation:

The output of the above program will be ef in Windows (Turbo C) and ab in Linux (GCC).

Since C is a machine dependent language therefore sizeof(int) may return different values
in different operating system.

In Windows operating system sizeof(int)=2 bytes.

In Linux operating system sizeof(int)=4 bytes.

The given size of int is 2 bytes therefore program output is based on the Windows (Turbo C)
compiler. Therefore the output of program is ef.

12) Find out the error in the below program?


1. #include<stdio.h>
2.
3. int main()
4. {
5. int b=15;
6. void f1();
7. b = f1();
8. printf("%d\n", b);
9. return 0;
10. }
11. void f1()
12. {
13. printf("Hello");
14. }
a. Error: Doesn't print anything
b. Error: Not allowed assignment
c. No error
d. None of the above

Hide Answer

The correct option is (b).

Explanation:
The void f() function is not visible to the compiler while going through main() function.

Hence we need to declare this prototype void f(); before the main() function. This kind of
error is not occurring in modern compiler.

Therefore on compiling the above program it give Error: Not allowed assignment.

13) Which statement is correct about the below program?


1. #include<stdio.h>
2.
3. int main()
4. {
5. printf("%p\n", main());
6. return 0;
7. }
a. Error: main() cannot be called inside printf()
b. It prints the garbage values infinitely
c. Runs infinitely without printing anything
d. No Error and print nothing

Hide Answer

The correct option is (c).

Explanation:

In printf statement i.e. printf("%p\n", main()); This statement calls the main() function and
then it repeats infinitely, until the stack is overflow.

Therefore program runs infinitely without printing anything.

14) Functions can only be called either by value or reference.


a. True
b. False

Hide Answer

The correct option is (a).

Explanation:
A function can be called either using call by reference or call by value. Therefore the above
statement is true.

For Example:

Call by reference meansc=sub(&x, &y); here the address of x and y are passed.

Call by value means c= sub(x, y); here the value of x and y are passed.

15) If two "return" statements are used in a function successively,


the compiler will generate warnings.
a. True
b. False

Show Answer

16) What is the output of below program?


1. #include<stdio.h>
2. int chk (int, int);
3.
4. int main()
5. {
6. int x;
7. x = check(10, 20);
8. printf("x=%d\n", x);
9. return 0;
10. }
11. int check(int a, int b)
12. {
13. int *y, *z;
14. y=&a;
15. z=&b;
16. a>=45 ? return(*y): return(*z);
17. }
a. Print 1
b. Print 10
c. Print 20
d. Compile error
Hide Answer

The correct option is (d).

Explanation:

There is an error in the line 16 i.ea>=45 ? return(*y): return(*z); we are not allowed to
use return keyword in the ternary operator

Therefore compile error occur in the output of a program

17) What is the output of below program?


1. #include<stdio.h>
2. int j;
3. int function();
4.
5. int main()
6. {
7. while(j)
8. {
9. function();
10. main();
11. }
12. printf("Hi\n");
13. return 0;
14. }
15. int function()
16. {
17. printf("Hello");
18. }
a. Hi
b. Hello Hi
c. No output
d. Infinite loop

Hide Answer

The correct option is (a).

Explanation:

Step 1: int j; The variable j is declared as an integer type.


Step 2: int function(); This statement tells the compiler that the function does not accept
any argument and it returns an integer value.

Step 3: while(j) The value of j is not initialized so the while condition is failed. Therefore it
does not execute the while block.

Step 4: printf("Hi\n"); This statement prints "Hi".

Therefore the output of the program is "Hi".

18) The C library function rewind() is used for re-position the file
pointer at the beginning of the file.
a. True
b. False

Hide Answer

The correct option is (a).

Explanation:

In C, rewind function reposition the file position at the beginning of the file of given stream.

The syntax of using function rewind() is:

void rewind(FILE *stream)

This function also erases the error and end-of-file indicators for stream.

19) Which header file is used for supporting the functions-


malloc() and calloc().
a. stdio.h
b. math.h
c. stdlib.h
d. memory.h

Hide Answer

The correct option is (c).

Explanation:

void *calloc(size_h nitems, size_h size): Function calloc() requires the double argument.
void *malloc(size_h size) : Function malloc() requires the single argument.

For supporting the function- malloc() and calloc() stdlib.h header file is required.

20) A function can execute faster than a macro.


a. True
b. False

Hide Answer

The correct option is (b).

Explanation:

Macro can be executed faster with no overhead of context switch because the code of macro
gets expanded at the line of call.

Therefore, the above statement is false.

C Array Test 1

1) In C, if we pass an array as an argument to a function, what


actually get passed?
a. Address of the last element of array
b. Base address of the array
c. Value of elements in array
d. First element of the array

Hide Answer

The correct option is (b).

Explanation:

In C language when we pass an array as a function argument, then the Base address of
the array will be passed.

2) What will be the output of the below program?


1. #include<stdio.h>
2. main()
3. {
4. char x[]="javaTpoint", y[]="javaTpoint";
5. if(x==y){
6. printf("Strings are Equal");
7. }
8. }
a. Strings are Equal
b. No output
c. Runtime error
d. Compilation error

Hide Answer

The correct option is (a).

Explanation:

In the program we are comparing the base address of 'x' and 'y' and they are not same.

Therefore the program has No output.

3) What will be the output of the below program?


1. #include<stdio.h>
2. main(){
3. char x[] = "Hi\0Hello";
4. printf("%d %d", strlen(x), sizeof(x));
5. }
a. 59
b. 9 20
c. 2 9
d. 2 5

Hide Answer

The correct option is (c).

Explanation:
The strlen(x) function is used for finding the length of string 'x'. In program the length of
string is count of character upto '\0'. Hence the string length output is 2.

The sizeof(x) function is used for finding the size of string 'x'. In program sizeof() returns
the size of the complete array. Hence the size of array output is 9.

Therefore the combined output of the program is 2 9.

4) A pointer to a block of memory is effectively same as an array.


a. True
b. False

Hide Answer

The correct option is (a).

Explanation:

Using the standard library function malloc() and treat it as an array. The value of array is
same as pointer to a block of memory.

Therefore it is possible to allocate block of memory at run time in array.

5) Which of the following statements are correct about array in C?


1. The expression num[2] represents the very second element in the array
2. The declaration of num[SIZE] is allowed if SIZE is a macro
3. The array of int num[20]; can store 20 elements
4. It is necessary to initialize array at the time of declaration

a. 2
b. 2,3
c. 1,4
d. 2,4

Hide Answer

The correct option is (b).

Explanation:

1. The expression num[2] represents the second element in array. This statement is
false, because it represents the third element of the array.
2. The declaration of num[SIZE] is allowed if SIZE is a macro. This statement is true,
because MACRO is used for replacing the symbol size with given value.
3. The array of int num[20]; can store 20 elements. This statement is true
4. It is necessary to initialize array at the time of declaration. This statement is false

Therefore statements '2' and '3' are correct.

6) What will be the output of the below program?


1. #include<stdio.h>
2. main(){
3. int x[] = {100, 200, 300};
4. printf("%d", *x +1);
5. }
a. 100
b. 200
c. 101
d. 201

Hide Answer

The correct option is (c).

Explanation:

In program *x refers to 100 and adding a 1 to *x gives 101.

Therefore the output is101.

7) In the below statement, what does the "arr" indicates?


1. char *arr[20];
a. arr is an array of 20 characters
b. arr is an array of 20 character pointers
c. arr is an array of function
d. arr is a pointer to an array

Hide Answer

The correct option is (b).


Explanation:

The Square parenthesis signifies an array at declaration and the type is char *. So it is an
array of character pointer.

Therefore "arr" is an array of 20 character pointers.

8) What will be the output of the below program?


1. #include<stdio.h>
2. void main()
3. {
4. char a[] = "C++";
5. printf("%s ",a);
6. a++;
7. printf("%s",a);
8. }
a. C++ ++
b. ++ ++
c. C++ C++
d. Compile error

Hide Answer

The correct option is (d).

Explanation:

In program 'a' refers to constant address and the constant address variable is not allowed to
be incremented.

Therefore the program will generate compile error in output.

9) Which of the statements are correct about 5 used in the


program?
1.
2. int num[5];
3. num[5]=20;
a. In the first statement 5 specifies an array size, whereas in the second statement it
specifies a particular element of array.
b. In the first statement 5 specifies a particular element, whereas in the second
statement it specifies a array size.
c. In the first statement 5 specifies a particular element, whereas in the second
statement it specifies a type.
d. In both the statement 5 specifies array size.

Hide Answer

The correct option is (a).

Explanation:

The statement int num[5]; specifies the size of array and num[5]=20; specifies the
particular element (6th element) of the array.

Therefore in first statement 5 specifies an array size, whereas in second element it specifies
a particular element of an array.

10) Which of the below statements using the name of an array


does not yield the base address?
1. When array name is operand of the & operator
2. When array name is passed to scanf() function
3. When array name is passed to printf() function
4. When array name is used with the sizeof operator.

a. 1, 4
b. 4
c. 1
d. 1, 3

Hide Answer

The correct option is (a).

Explanation:

The statement 1 and 4 does not yield the base address of an array. While the printf() and
scanf() yields the base address of an array.

11) Which statements are correct about the program given


below?
1. #include<stdio.h>
2. int main()
3. {
4. int size, j;
5. scanf("%d", &size);
6. int arr[size];
7. for(j=1; j<=size; j++)
8. {
9. scanf("%d", arr[j]);
10. printf("%d", arr[j]);
11. }
12. return 0;
13. }
a. The code is erroneous since the values of an array are getting scanned through the
loop.
b. The code is erroneous since the statement declaring an array is invalid.
c. The code is erroneous since the subscript for an array used in for loop is in the range
1 to size.
d. The code is correct and runs successfully.

Hide Answer

The correct option is (b).

Explanation:

In program the statement int arr[size]; produces an error, because we cannot initialize the
size of array dynamically. Constant expression is required for initialize the size of array. For
Example: int arr[20];

Therefore the code is erroneous since the statement declaring an array is invalid.

12) Are the expression &arr and arr different for an array of 15
integers?
a. Yes
b. No

Hide Answer

The correct option is (a).

Explanation:
Yes, both mean two different things. 'arr' gives the address of first int, whereas the '&arr'
gives the address of array of ints.

Therefore the expression '&arr' and 'arr' are different for an array of 15 integers.

13) Is there any difference in the below declarations?


1. int fun (int arr[5]);
2. int fun(int arr[]);
a. Yes
b. No

Hide Answer

The correct option is (b).

Explanation:

No, both the declarations are same. It is a prototype for function fun() that accepts one
integer array as parameter and an integer value is return.

14) What will be the output of the below program?


1. #include<stdio.h>
2. int main()
3. {
4. int arr[2]={20};
5. printf("%d\n", 0[arr]);
6. return 0;
7. }
a. 2
b. 0
c. 20
d. 16

Hide Answer

The correct option is (c).

Explanation:

Step 1: int arr[2]={20}; The variable arr[2] is declared as an integer array with size of '3'
and it's first element is initialized with value '20'(means arr[0]=20)
Step 2: printf("%d\n", 0[arr]); It prints the first element value of variable 'arr'.

Therefore the output of the program is 20.

15) What will be the output of the program if an array begins with
address 65486?
1. #include<stdio.h>
2. int main()
3. {
4. int arr[] = {10, 11, 12, 15, 23};
5. printf("%u, %u\n", arr, &arr);
6. return 0;
7. }
a. 65486, 65486
b. 65486, 65490
c. 65486, 65487
d. 65486, 65488

Hide Answer

The correct option is (a).

Explanation:

Step 1: int arr[] = {10, 11, 12, 15, 23}; The variable 'arr' is declared as an integer array
and initialized.

Step 2: printf("%u, %u\n", arr, &arr); Here, the base address of the array is 65486.

Hence the arr, &arr is pointing towards the base address of the array arr.

Hence the output of the program is 65486, 65486

16) What is 'y' in the below program?


1. #include<stdio.h>
2. int main()
3. {
4. typedef char (*(*arrfptr[4])())[20];
5. arrfptr y;
6. return 0;
7. }
a. 'x' is an array of three pointers
b. 'x' is an array of three function pointers
c. 'x' is a pointer
d. Error in 'x' declaration

Hide Answer

The correct option is (b).

Explanation:

The statement typedef char (*(*arrfptr[4])())[20]; means arfptr is an array of 3 function


pointer which will return an array of 20 dimension whose data type is char.

Therefore 'x' is an array of three function pointers.

17) The return keyword is used for transfer control from a function
back to the calling function.
a. Yes
b. No

Hide Answer

The correct option is (a).

Explanation:

In C, the return function stops the execution of function and returns the control with value
to the calling function. Execution is begins in calling function by instantly following the call.

18) What will be the output of the below program?


1. #include<stdio.h>
2. main()
3. {
4. struct { int y;} var = {4}, *a = &var;
5. printf("%d %d %d",var.y,a->y,(*a).y);
6. }
a. 4 4 garbage value
b. 4 4 0
c. 4 4 4
d. Compile error

Hide Answer

The correct option is (c).

Explanation:

The two possible methods of accessing structure elements using pointer is by using * or ->
(arrow operator).

Therefore the output of a program is 4 4 4.

19) What will be the output of the below program?


1. #include<stdio.h>
2. main()
3. {
4. int a[3] = {1,,2};
5. printf("%d", a[a[0]]);
6. }
a. 1
b. 2
c. 0
d. Compile error

Hide Answer

The correct option is (d).

Explanation:

In the program invalid syntax is used for initializing the array. Therefore compile error
occurs in the output of a program.

20) Find out whether both the loops in a program prints the
correct string length?
1. #include<stdio.h>
2. main()
3. {
4. int j;
5. char s[] = "javaTpoint";
6. for(j=0; s[j]; ++j);
7. printf("%d \n", j);
8.
9. j=0;
10. while(s[j++]);
11. printf("%d ", j);
12. }
a. Yes, both the loops prints the correct string length
b. Only while loop prints the correct string length
c. Only for loop prints the correct string length
d. Compile error in the program

Hide Answer

The correct option is (c).

Explanation:

1. Output: 10
2. 11

In while loop the incorrect string length is printed because while loop variable 'i' get
incremented after checking for '\0', hence giving 1 more than the length of string.

Therefore only for loop prints the correct string length.

next →← prev

C Pointers Test 1

1) In a structure, if a variable works as a pointer then from the given below


which operator is used for accessing data of the structure using the variab
a. %
b. ->
c. .
d. #

Hide Answer

The correct option is (b).

Explanation:

For a structure, Arrow ( ->) is used for access the data using pointer variable and Dot(.) operator can b
accessing the data using normal structure variable.

2) For the array element a[i][j][k][2], determine the equivalent pointer expre
a. *(*(*(*(a+i)+j)+k)+2)
b. *( ((a+m)+n+o+p)
c. ((((a+m)+n)+o)+p)
d. *( (((a+m)+n)+o+p)

Hide Answer

The correct option is (a).

Explanation:

For the array element a[i][j] the pointer expression is *(*(a+i)+j)

For the array element a[i][j][k] the pointer expression is *(*(*(a+i)+j)+k)

For the array element a[i][j][k][2] the pointer expression is *(*(*(*(a+i)+j)+k)+2)

3) Are the expression ++*ptr and *ptr++ are same?


a. True
b. False

Hide Answer

The correct option is (b).


Explanation:

++*ptr increments the value pointed by ptr and*ptr++ increments the pointer not the value.

4) Select the correct statement which is a combination of these two statem


1. Statement 1: p= (char*) malloc(100);
2. Statement 2: char *p;
a. char *p = (char*)malloc(100);
b. char *p = (char) malloc(100);
c. char p = *malloc(100);
d. None of the above

Hide Answer

The correct option is (a).

Explanation:

The below code is a prototype of malloc() function, here ptr indicates the pointer.

1. ptr = (data type *)malloc(size);

In below code, "*p" is a pointer of data type char and malloc() function is used for allocating the memor

1.
2. char *p = (char*)malloc(100);

5) For the below mention C statement, what is your comment?


1. signed int *p=(int*)malloc(sizeof(unsigned int));
a. Would throw Runtime error
b. Improper typecasting
c. Memory will be allocated but cannot hold an int value in the memory
d. No problem with the statement

Hide Answer
The correct option is (d).

Explanation:

The size of int and unsigned data type is same therefore there is no problem in a C statement:

signed int *p=(int*)malloc(sizeof(unsigned int));

6) What will be the output of a program?


1. #include<stdio.h>
2. #include<string.h>
3. int main()
4. {
5. int j, k;
6. char *z="Hello ";
7. k = strlen(z);
8. *z = z[k];
9. for(j=0; j<=k; j++)
10. {
11. printf("%s ", z);
12. z++;
13. }
14. printf("\n", z);
15. return 0;
16. }
a. Hello
b. olleH
c. ello llo lo o
d. Hello ello llo lo o

Hide Answer

The correct option is (c).

Explanation:

As C-Language is machine dependent language. Therefore it will give different output in


other platforms (depending upon machine and compiler).
If we execute and compile this program in Windows platform using Turbo C, then it will give
output "ello llo lo o".

If we execute and compile this program in Linux platform using GCC Compiler, then it will
give output "Hello ello llo lo o".

7) How many bytes are occupied by far, near and huge pointers in
DOS (Disk Operating System)?
a. near=4 far=8 huge=8
b. near=4 far=4 huge=8
c. near=2 far=4 huge=4
d. near=2 far=4 huge=8

Hide Answer

The correct option is (c).

Explanation:

Under Linux and Windows every pointer is 4 bytes long.

Under DOS the value of near, far and huge pointer is:

o near=2
o far=4
o huge=4

8) What will be the output of a program?


1. #include<stdio.h>
2. int main()
3. {
4. int *i;
5. void fun(int**);
6. fun(&i);
7. return 0;
8. }
9. void fun(int **j)
10. {
11. int b=10;
12. /* Statement is add here */
13. }
a. **j=b
b. j=&b
c. *j=&b
d. &j=*b

Hide Answer

The correct option is (c).

Explanation:

In normal pointer assignment, when function (fun()) is called it's received as (j), so
whatever operation performed on j will be returned at i in main.

After the function call,

1. &i=**j
2. i=*j
3. In function fun(),
4. *j=&b
5. So, i=&b

Therefore the address of (b) get stored in (i) when we use statement *j=&b.

9) The below program reports an error on compilation.


1. #include<stdio.h>
2. int main()
3. {
4. float j=12, *i;
5. void *a;
6. a=&j;
7. i=a;
8. printf("%f\n", *i);
9. return 0;
10. }
a. True
b. False

Hide Answer
The correct option is (b).

Explanation:

There is no error on compilation of the program in GCC under Linux and Turbo C under
DOS.

The output of the program is 12.000000.

10) Are the three declarations char **ball, char *ball[], and char
ball[][] same?
a. True
b. False

Hide Answer

The correct option is (b).

Explanation:

No, the three declarations char **ball, char *ball[], and char ball[][] are different.

char **ball - It is a double pointer

char *ball[] -It is an array of pointers

char ball[][]-It is 2-d array.

11) According to ANSI specification, how to use main() function


with command-line arguments?
a. int char main(int argc, *argv)
b. int main (int argc, char *argv[])
c. int main() { int char (*argv argc); }
d. None of the above

Hide Answer

The correct option is (b).

Explanation:

In some cases it is necessary to give command line values to the C programming for
executing the particular code when the code of program is controlled from outside. This
command line values are known as command line arguments. The command line arguments
are handled by main() function.

Use of main() with command line argument is,

int main (int argc, char *argv[])

Here, argv[] refers to the pointer array which points to each argument passed to the
program and argc is a number of argument passed.

12) What is the error in below C statement?


1. signed int *p=(int*)malloc(sizeof(unsigned int));
a. Memory will be allocated but cannot hold an int value in the memory
b. Improper type casting
c. Would throw Runtime error
d. No issue with statement

Hide Answer

The correct option is (d).

Explanation:

In given C statement the size of int and unsigned is same. Hence, there is no problem in
allocating the memory.

Therefore the given C statement has no error.

13) Choose the correct option for the below program.


1. #include<stdio.h>
2. main()
3. {
4. int *a, **b;
5. printf("%u\n", sizeof(a));
6. printf("%u\n", sizeof(b));
7. }
a. First printf() prints the value less than the second
b. Second printf() prints the value less than the first
c. Both the printf() will print the same value
d. Error in the code
Hide Answer

The correct option is (c).

Explanation:

Every type of pointer variable occupies the same amount of memory irrespective of any
data type.

Therefore both the printf() statement will print the same value.

14) What will be the output of the below program?


1. #include<stdio.h>
2. void main()
3. {
4. char *a = "C++";
5. printf("%s ", a);
6. a++;
7. printf("%s", a);
8. }
a. C++ ++
b. C++ C++
c. ++ ++
d. Compile error

Hide Answer

The correct option is (a).

Explanation:

First print statement i.e. printf("%s ", a); used for printing the value stored in pointer a.
Hence the C++ is printed in output.

After a++, a points the string "++".

Hence, the second print statement i.e. printf("%s ", a); prints the ++ in output.

Therefore the combined output of the program is C++ ++.

15) In the below statement, what does the "pf" indicates?


1. int (*pf)();
a. pf is a pointer
b. pf is a function pointer
c. pf is a pointer of function which returns int
d. None of the above

Hide Answer

The correct option is (c).

Explanation:

In the given statement "pf" is a pointer as well as it holds some function reference.

Therefore "pf" indicate the pointer of function which returns int.

16) What will be the output of the program given below?


1. #include<stdio.h>
2. main()
3. {
4. char *x = NULL;
5. printf("%c", *x);
6. }
a. 0
b. NULL
c. Compile error
d. Runtime error

Hide Answer

The correct option is (c).

Explanation:

In the program x points the NULL address. It is invalid to access the NULL address hence
the program gives Runtime error.

Therefore the output of the program is Runtime error.

17) Is this a right way for NULL pointer assignment?


1. int j=0;
2. char *p=(char*)j;
a. Yes
b. No

Hide Answer

The correct option is (b).

Explanation:

The above null pointer assignment method is incorrect.

The correct way is:

1. char *p=0 (or) char *p=(char*)0

18) Will the program be compiled in Turbo C?


1. #include<stdio.h>
2. int main()
3. {
4. int b=10, *i;
5. void *p;
6. i=p=&b;
7. i++;
8. p++;
9. printf("%u %u\n",i, p);
10. return 0;
11. }
a. Yes
b. No

Hide Answer

The correct option is (b).

Explanation:

In statement p++ error occur because we cannot perform arithmetic operation


on void pointers.

The following error is display on compiling the above program in TurboC.


Compiling PROGRAM.C:

Error PROGRAM.C 8: Size of the type is unknown or zero.

19) Which statement is correct about the program given below?


1. #include<stdio.h>
2. int main()
3. {
4. int j=10;
5. int *i=&j;
6. return 0;
7. }
a. j is a pointer to an int and stores address of i
b. i is a pointer to a pointer to an int and stores address of j
c. i and j are pointers to an int
d. i is a pointer to an int and stores address of j

Hide Answer

The correct option is (d).

Explanation:

In program 'i' is the variable contain pointer. So it is pointer variable and points toward an
integer type in memory location. Therefore 'i' is a pointer to an int.

Now the address of 'j' is assigned to the 'i' pointer, i.e. address of 'j' store to 'i' location.

Therefore the 'i' is a pointer to an int and it stores the address of 'j'.

20) What will be the output of the program given below?


1. #include<stdio.h>
2. main()
3. {
4. char *p= "Xyz";
5. while(*p)
6. printf("%c", *p++);
7. }
a. Xyz
b. yz
c. Runtime error
d. Compile error

Hide Answer

The correct option is (a).

Explanation:

In program while loop continues until *s is not equal to '\0'. Inside loop character is fetched
first and address is incremented later.

Therefore the print statement i.e. printf("%c", *p++); will print the Xyz in output.

C String Test 1

1) Which of the function is more appropriate for reading a multi-


word string?
a. puts()
b. gets()
c. printf()
d. scanf()

Hide Answer

The correct option is (b).

Explanation:

The function gets() is used for collecting a string of characters terminated by new line from
the standard input stream stdin.

Therefore gets() is more appropriate for reading a multi-word string.

2) Which library function can change an unsigned long integer to


a string?
a. system()
b. ltoa()
c. ultoa()
d. unsigned long can't be change into a string

Show Answer

3) What is the value return by strcmp() function when two strings


are the same?
a. 2
b. 1
c. 0
d. Error

Hide Answer

The correct option is (c).

Explanation:

C library function strcmp() compares the two strings with each other and the value is return
accordingly.

1.
2. int strcmp (const char *str1, const char *str2)

Comparison occurs between a first string (str1) with a second string (str2).

On comparing the two string, the values return by a function strcmp() are:

o If, str1 is equal to str2 then Return value = 0


o If, str1 is greater than str2 then Return value > 0
o If, str1 is less than str2 then Return value < 0

4) What is built in library function for comparing the two strings?


a. strcmp()
b. equals()
c. str_compare()
d. string_cmp()

Hide Answer

The correct option is (a).


Explanation:

The strcmp() is a built-in function available in "string.h" header file. It is used for comparing
the two strings. It returns 0 if both are same strings. It returns positive value greater than 0
if first string is greater than second string, otherwise it returns negative value.

5) What will be the output of the below program?


1. #include<stdio.h>
2. int main()
3. {
4. char a[] = "%d\n";
5. a[1] = 'b';
6. printf(a, 65);
7. return 0;
8. }
a. b
b. a
c. A
d. 65

Hide Answer

The correct option is (c).

Explanation:

Step 1: char a[] = "%d\n"; The variable 'a' is declared as an array of characters and
initialized with string "%d".

Step 2: a[1] = 'b'; Here, we overwrite the second element of array ?a? by 'b'. Hence the
array ?a? becomes "%c".

Step 3: printf(a, 65); becomes printf("%c", 65);

Therefore it will print the ASCII value of 65. Hence the output is 'A'.

6) What will be the output of the below program?


1. #include<stdio.h>
2. #include<string.h>
3. int main()
4. {
5. printf("%d\n", strlen("javaTpoint"));
6. return 0;
7. }
a. 9
b. 10
c. 11
d. Compile error

Hide Answer

The correct option is (b).

Explanation:

The function strlen() returns the number of character in the string.

Therefore, strlen("javaTpoint") returns 10

Hence the output of the program is "10".

7) Which of the following statements are correct about string?


1. The format specifier %s is used to print a string.
2. The length of the string can be obtained by strlen().
3. The pointer cannot work on string.
4. A string is a collection of characters terminated by '\0'.

a. 1, 4
b. 1, 2, 3
c. 1, 2, 4
d. 2, 3, 4

Hide Answer

The correct option is (c).

Explanation:

Clearly the statement 1, 2 and 4 are correct about the string but the statement 3 is
incorrect because we can use pointer on strings.

For example: char *p="javaTpoint"


8) For the following statements will arr[2] and ptr[2] fetch the
same character?
1. char arr[] = "javaTpoint"
2. char *ptr = "javaTpoint"
a. Yes
b. No

Hide Answer

The correct option is (b).

Explanation:

Yes, for arr[2] and ptr[2] both the statements will prints the same character 'v'.

9) Which of the statements are correct about the below program?


1. #include<stdio.h>
2. int main()
3. {
4. char stri[20], *p;
5. printf("Enter the string\n:");
6. scanf("%s", stri);
7. p=stri;
8. while(*p != '\0')
9. {
10. if(*p >= 97 && *p <= 122)
11. *p = *p-32;
12. p++;
13. }
14. printf("%s",stri);
15. return 0;
16. }
a. The code converts lower case character to upper case
b. The code converts upper case character to lower case
c. The code converts a string to an integer
d. Compile Time error

Hide Answer
The correct option is (a).

Explanation:

The program converts the enter string to upper case string.

Output:

1.
2. Enter the string: javaTpoint
3. JAVATPOINT

10) Determine the wrong file opening mode from the following.
a. w
b. a
c. x
d. r

Hide Answer

The correct option is (c).

Explanation:

File opening mode "x" is incorrect because there is no such mode exists for file opening
operation in C language.

11) Function fopen() with the mode "r+" used to open a file for
_______
a. reading and adding new content
b. reading and writing
c. it works only for directory
d. only for reading

Hide Answer

The correct option is (b).

Explanation:

Function fopen() opens the file and mode "r+" used for checking the file should exist and
opens for both reading writing operation.
Therefore in file handling reading and writing operation is performed by function fopen()
with the mode "r+".

12) Which is used in mode string for opening the file in binary
mode?
a. a
b. B
c. b
d. bin

Hide Answer

The correct option is (c).

Explanation:

For opening the file in binary mode the alphabet 'b' is used in mode string. To perform
unformatted data I/O a file is opened in binary mode.

13) Which of the following statement is correct?


a. strcmp(s1, s2) returns 0 if s1==s2
b. strcmp(s1, s2) returns 1 if s1==s2
c. strcmp(s1, s2) returns a number less than 0 if s1>s2
d. strcmp(s1, s2) returns a number greater than 0 if s1<s2

Hide Answer

The correct option is (a).

Explanation:

On comparing the two string, the values return by a function strcmp() are:

o If, str1 is equal to str2 then Return value = 0


o If, str1 is greater than str2 then Return value > 0
o If, str1 is less than str2 then Return value < 0

The strcmp() return an int value and from the given statement only statement (a) is correct
i.e. strcmp(s1, s2) returns 0 if s1==s2

14) What will be the output of the below program?


1. #include<stdio.h>
2. #include<string.h>
3. int main()
4. {
5. char stri[] = "Java\0\Tpoint\0";
6. printf("%s\n", stri);
7. return 0;
8. }
a. Tpoint
b. Java
c. Java Tpoint
d. Java\0Tpoint

Hide Answer

The correct option is (b).

Explanation:

The string is a collection of characters terminated by '\0'.

Step 1: char stri[] = "Java\0\Tpoint\0"; The variable stri is declared as an array of


characters and it is initialized with value "Java".

Step 2: printf("%s\n", stri); it print the value of stri.

Therefore the output of the program isJava.

15) What will be the output of the below program?


1. #include <stdio.h>
2. void main()
3. {
4. char ch[10]={'h', 'e', 'l', 'l', 'o', '\0'};
5. char ch2[10]={'c', '\0'};
6. strcat(ch,ch2);
7. printf("Value of first string is: %s",ch);
8. }
a. hello
b. c
c. helloc
d. Compile error
Hide Answer

The correct option is (c).

Explanation:

The strcat() function is used for string concatenation. The strcat(first_string,


second_string) function concatenates two strings and result is returned to first_string.

Therefore the output of the program is helloc.

16) What will be the output of the below program?


1. #include<stdio.h>
2. #include<conio.h>
3. #include<string.h>
4. void main(){
5. char str[100]="this is javatpoint with c and java";
6. char *sub;
7. clrscr();
8. sub=strstr(str,"java");
9. printf("\n %s",sub);
10. getch();
11. }
a. this is javatpoint with c and java
b. javatpoint with c and java
c. java
d. compile error

Hide Answer

The correct option is (b).

Explanation:

The strstr() function returns pointer to the first occurrence of the matched string in the
given string. It is used to return substring from first match till the last character.

Syntax:

char *strstr(const char *string, const char *match)

string: It represents the full string from where substring will be searched.

match: It represents the substring to be searched in the full string.


Therefore the output of the above program is javatpoint with c and java.

17) What will be the output of the below program?


1. #include<stdio.h>
2. main()
3. {
4. char x[] = "Hi\0Hello";
5. printf("%d %d", strlen(x), sizeof(x));
6. }
a. 59
b. 9 20
c. 2 9
d. 2 5

Hide Answer

The correct option is (c).

Explanation:

The strlen(x) function is used for finding the length of string 'x'. In program the length of
string is count of character upto '\0'. Hence the string length output is 2.

The sizeof(x) function is used for finding the size of string 'x'. In program sizeof() returns
the size of the complete array. Hence the size of array output is 9.

Therefore the combined output of the program is 2 9.

18) If the size of pointer is 4 bytes then what will be the output of
below program?
1. #include<stdio.h>
2. int main()
3. {
4. char *stri[] = {"Java", "C", "Android", "Embedded", "JS"};
5. printf("%d, %d", sizeof(stri), strlen(stri[0]));
6. return 0;
7. }
a. 22, 4
b. 20, 4
c. 20, 5
d. 25,4

Hide Answer

The correct option is (b).

Explanation:

Step 1: char *stri[] = {"Java", "C", "Android", "Embedded", "JS"}; The variable stri is
declaraed as a pointer to the array of 5 strings.

Step 2: printf("%d, %d", sizeof(stri), strlen(stri[0]));

Sizeof(stri) denotes 5*4 bytes=20 bytes. Hence it prints '20'

Strlen(stri[0]) becomes strlen(Java). Hence it prints '4'.

Hence the output of the program is 20, 4.

19) What will be the output of below program?


1. #include<stdio.h>
2. #include<string.h>
3. int main()
4. {
5. printf("%c\n", "javatpoint"[5]);
6. return 0;
7. }
a. o
b. t
c. p
d. Compile error

Hide Answer

The correct option is (c).

Explanation:

The statement printf("%c\n", "javatpoint"[5]); prints the 6th character of the string
"javatpoint".

Hence the output of a program is 'p'.


20) What will be the output of the below program in 16 bit platform
assuming that 2022 is memory address of the string "Welcome"
(in Turbo C under DOS)?
1. #include<stdio.h>
2. int main()
3. {
4. printf("%u %s\n", &"Welcome", &"Programming");
5. return 0;
6. }
a. 2022 Programming
b. Welcome Programming
c. Welcome 2022
d. Compile error

Hide Answer

The correct option is (a).

Explanation:

In a statement printf("%u %s\n", &"Welcome", &"Programming");

The %u format specifier tells the compiler to print the memory address of the "Welcome".

The %s format specifier tells the compiler to print the string "Programming".

Hence the output of the program is "2022 Programming".

C Structure, Union, Enums Test 1

1) What is the similarity between a structure, union and


enumeration?
a. All are useful in defining new data types
b. All are useful in defining new variable
c. All are useful in defining new structures
d. All are useful in defining new pointers
Hide Answer

The correct option is (a).

Explanation:

A structure, enumeration and union all of them can be helpful in defining a new data types
in C language.

It is used for creating new data types that holds all kinds of data type like int, char, float,
array inside a user defined data type. So user can use new values and logic of operation in
simple manner.

2) How will we free the allocated memory in a C program?


a. delete(var-name)
b. dalloc(var-name)
c. remove(var-name)
d. free(var-name)

Hide Answer

The correct option is (d).

Explanation:

The built in function free(var-name) is used for free or clear the memory space. If we use
free() the referred memory location can be released for the future use or other operations.

Therefore the free(var-name) is used for clear the allocated memory in a C program.

3) A union can be nested in a structure.


a. True
b. False

Hide Answer

The correct option is (a).

Explanation:

In address mapping or allocation of structure, the program takes union as a one data type
in it.

Therefore union can be nested in a structure statement is true.


4) What will be the output of the below program?
1. #include<stdio.h>
2. main()
3. {
4. union abc {
5. int a;
6. char cha;
7. }var;
8. var.cha = 'A';
9. printf("%d", var.a);
10. }
a. A
b. 65
c. 97
d. Garbage value

Hide Answer

The correct option is (b).

Explanation:

The union variable share the common memory for all its elements 'a' gets 'A' whose ASCII
value is 65.

The statement printf("%d", var.a); is used for printing the value 65 in output.

Therefore the output of the program is 65.

5) The elements of union and structure are always accessed


using & operator.
a. Yes
b. No

Hide Answer

The correct option is (b).

Explanation:

No, because the elements of union and structure are always accessed using dot(.) operator.
6) What will be the output of the below program?
1. #include<stdio.h>
2. int main()
3. {
4. union b
5. {
6. int j;
7. char ch[2];
8. };
9. union b un;
10. un.ch[0]=3;
11. un.ch[1]=2;
12. printf("%d, %d, %d\n", un.ch[0], un.ch[1], un.j);
13. return 0;
14. }
a. 3, 2, 5
b. 515, 515, 4
c. 3, 2, 515
d. 515, 2, 3

Hide Answer

The correct option is (c).

Explanation:

In a program system will allocate 2 bytes for the union.

The statements un.ch[0]=3; un.ch[1]=2; store the data in memory as shown below:

Therefore the output of program is 3, 2, 515.

7) What will be the output of the below program?


1. #include<stdio.h>
2.
3. int main()
4. {
5. enum months {JAN=-1, FEB, MARCH=6, APRIL, MAY, JUNE};
6. printf("%d, %d, %d, %d, %d, %d\n", ++JAN, FEB, MARCH, APRIL, MAY, JUNE);
7. return 0;
8. }
a. 0, 1, 6, 3, 4, 5
b. -1, 0, 1, 2, 3, 4
c. 0, 0, 6, 7, 8, 9
d. Compile Error

Hide Answer

The correct option is (d).

Explanation:

Increment and decrement operations are not allowed to be performed in user defined data
type.

Since enum is a user defined data type, no operations can be performed on user-defined
data types.

Therefore ++ or -- logical operations cannot be done on enum value and the program will
return compile error in the output on using this operations in enum data type.

8) Find out the error in the below program?


1. struct employ
2. {
3. int ecode;
4. struct employ e;
5. };
a. Linked Error
b. Error: in structure declaration
c. No error
d. None of the above

Hide Answer

The correct option is (b).


Explanation:

The structure employ contains a member 'e' of a same data type i.e. struct employ.

In this stage the compiler does not know the size of structure.

Therefore the compiler returns Error: in structure declaration.

9) Find out the error in the below program?


1. #include<stdio.h>
2. int main()
3. {
4. struct employ
5. {
6. char name[22];
7. int age;
8. float bs;
9. };
10. struct employ e;
11. e.name = "Nakul";
12. e.age = 22;
13. printf("%s %d\n", e.name, e.age);
14. return 0;
15. }
a. Error: invalid constant expression
b. Error: Rvalue required
c. Error: Lvalue required/incompatible types in assignment
d. No error, Output: Nakul 22

Hide Answer

The correct option is (c).

Explanation:

In program we assign string to a struct variable like e.name = "Nakul";

In C programming language we are not allowed to assign a string to struct variable.

We have to use strcpy(char *dest, const char *source) function for assigning a string
For example: strcpy(e.name, "Nakul");

Therefore the compiler returns Error: Lvalue required/incompatible types in


assignment

10) What will be the output of the below program?


1. #include<stdio.h>
2. main()
3. {
4. enum { GREAT, is=7, india };
5. printf("%d %d", GREAT, india);
6. }
a. 01
b. 0 2
c. 0 8
d. Compile error

Hide Answer

The correct option is (c).

Explanation:

In enum data type sequence always start with 0. If the value is assigned then sequence
continues from the assigned value.

Therefore 0 8 is the output of the given program

11) What is a size of the following union definition?


1. #include<stdio.h>
2. union xyz {
3. char x,y,z,a,b,c,d,e;
4. int i;
5. }xyz;
6. main()
7. {
8. printf( "%d", sizeof( xyz ));
9. }
a. 4
b. 8
c. 2
d. 1

Hide Answer

The correct option is (a).

Explanation:

Union is special data type used in C programming it allows to store different data types in
the same memory location.

All the element of union share the common memory and union size is biggest element size.

Therefore the output of the program or size of union definition is 4.

12) What will be the output of the below program?

a. 4 4 garbage value
b. 4 4 0
c. 4 4 4
d. Compile error

Hide Answer

The correct option is (c).

Explanation:

The two possible methods of accessing structure elements using pointer is by using * or ->
(arrow operator).

Therefore the output of a program is 4 4 4


13) If an integer is 2 bytes wide, a character is 1 byte wide and a
long integer is 4 bytes wide then will the following structure
always occupy 7 bytes?
1. struct employ
2. {
3. int j;
4. char ch;
5. long int a;
6. };
a. Yes
b. No

Hide Answer

The correct option is (b).

Explanation:

A compiler may leave the holes in structure by padding in the first char in a structure with
another byte for ensuring that the integer is stored at a location.

Also, there is 2 extra bytes after the integer to ensure that long integer is stored at
specified address which is multiple of 4.

Therefore the structure does not always occupy 7 bytes.

14) Which data type cannot be checked in switch-case


statement?
a. enum
b. character
c. integer
d. float

Hide Answer

The correct option is (d).

Explanation:
In C-language switch/casev statement is defined by the language specification to use
an int value therefore we cannot use a float value in switch/case statement.

15) In C language nested unions are allowed.


a. True
b. False

Hide Answer

The correct option is (a).

Explanation:

True, C language allows the use of nested union. Therefore we can use the union inside a
union in C program.

16) Find out the error in the below program?


1. struct employ
2. {
3. int ecode;
4. struct employ *e;
5. };
a. Error: in structure declaration
b. Linker
c. No error
d. None of the above

Hide Answer

The correct option is (c).

Explanation:

This type of declaration is known as self-referential structure. This type of declaration is


allowed to be used in a program.

Here *e is the pointer to a struct employ.


Therefore the compiler will return No error in a program

17) The Bit fields cannot be used in a union.


a. True
b. False

Hide Answer

The correct option is (b).

Explanation:

False, the bit fields are allowed to be used inside a union.

The C program using bit fields inside a union is given below:

1. #include<stdio.h>
2. union Pointer
3. {
4. unsigned int a:4;
5. unsigned int b:4;
6. int res;
7. };
8. int main()
9. {
10. union Pointer pt;
11. pt.a = 2;
12. pt.b = 6;
13. pt.res = pt.b;
14. printf("\n The value of res is: %d" , pt.res);
15. return 0;
16. }
17. // Output: The value of res is: 6

18) The union elements can be of different sizes.


a. True
b. False
Hide Answer

The correct option is (a).

Explanation:

True, the union element can be of different sizes.

All the union elements may have different sizes but they share the common space of
memory.

19) If the below structure is used for writing into a file using
fwrite(), can fread() read it back successfully?
1. struct employ
2. {
3. char *p;
4. int age;
5. };
6. struct employ e={"javaTpoint", 25};
7. FILE *fp;
8. fwrite(&e, sizeof(e), 1, fp);
a. Yes
b. No

Hide Answer

The correct option is (b).

Explanation:

No, because the structure in a program contain a char pointer while writing the structure to
the disk using fwrite() only the data stored in pointer 'n' will get written. Hence
the fread() fails to read the data stored in pointer.

Therefore the fread() cannot read it back successfully.

20) What will be the output of a program in Turbo C under DOS


(16 bit platform)?
1. #include<stdio.h>
2. int main()
3. {
4. struct values
5. {
6. int bit1:1;
7. int bit3:3;
8. int bit4:4;
9. }bit;
10. printf("%d \n", sizeof(bit));
11. return 0;
12. }
a. 9
b. 4
c. 2
d. 1

Hide Answer

The correct option is (c).

Explanation:

Since the C language is machine or compiler dependent language. In Turbo C(DOS) the size
of integer data type is 2.

Therefore the statement printf("%d \n", sizeof(bit)); prints the value 2 in output.

C Preprocessor Test 1

1) In which stage the below code gets replaced by the contents of


the file #include<stdio.h>
a. During linking
b. During editing
c. During preprocessing
d. During execution

Hide Answer
The correct option is (c).

Explanation:

During preprocessing stage the line #include<stdio.h> with the system header file of that
name gets replaced by the contents of file stdio.h.

Therefore the entire text of the file 'stdio.h' replaces with #include directive.

2) C preprocessor directive #undef can be used with a macro that


has been #define earlier.
a. True
b. False

Hide Answer

The correct option is (a).

Explanation:

True, the directive #undef can be used only with a macro that has been #define earlier in
a program.

For Example: #define PI 3.14

We can undefine PI macro by #undef PI

3) C preprocessor directive #ifdef...#elif?#endif is used for


conditional compilation.
a. True
b. False

Hide Answer

The correct option is (a).

Explanation:

True, the C macros like #ifdef...#elif?#endif are used for performing conditional operation
in C program.

The syntax of C preprocessor directive is:

1. #if <constant-expression>
2. #elif <constant-expression>
3. #endif

4) What will be the output of the below program?


1. #include<stdio.h>
2. #define SWAP(x, y) int t; t=x, x=y, y=t;
3. int main()
4. {
5. int x=10, y=20;
6. SWAP(x, y);
7. printf("x = %d, y = %d\n", x, y);
8. return 0;
9. }
a. x=10 , y=20
b. x=20, y=10
c. Error: Undefined symbol 't'
d. Error: Declaration not allowed in macro

Hide Answer

The correct option is (b).

Explanation:

The macro statement SWAP(x, y) int t; t=x, x=y, y=t; swaps the value of given two
variable.

Step 1: int x=10, y=20; The variable x and y are declared as an integer type and
initialized to 10, 20 respectively.

Step 2: SWAP(x, y);. Here the macro is substituted and it swaps the value to variable x
and y.

Hence the output of the program is x=20, y=10.

5) Which of the following are correctly formed #define statements


in C language?
a. #define CUBE(x) (X*X*X)
b. #define CUBE(X) {X*X*X}
c. #define CUBE (X) X*X*X
d. #define CUBE(X) (X)*(X)*(X)

Hide Answer

The correct option is (d).

Explanation:

The syntax for macro definition with argument is:

1. "#define MACRO_NAME(ARG) (ARG*ARG*ARG) "


o There should be no space between macro's name and it's '(args)'.
o The variables used as macro's argument are case sensitive and its expansion should
be same. i.e. 'x' and 'X' are different variables.
o A macro expansion should be enclosed within parenthesis '( )' (do not use { } or [ ]).

6) What will be the output of the below program?


1. #include <stdio.h>
2. #define DEF
3. int main(){
4. int j=3;
5. #ifdef DEF
6. printf("square of j=%d\n",j*j);
7. #else
8. printf("j=%d\n",j);
9. #endif
10. return 0;
11. }
a. square of j=9
b. j=3
c. Compile error
d. No output

Hide Answer

The correct option is (a).

Explanation:

In program #ifdef, #else and #endif are preprocessor commands.


If the macro DEF is defined by a #define statement, then the code immediately after
#ifdef is compiled otherwise code between #else and #endif commands gets executed.
Since, DEF has been defined, #ifdef DEF evaluates to true hence, square of 'j' is calculated
and gets printed i.e 9.

While defining the macro DEF, it is not compulsory to provide a expansion to the macro
statement because in a program we are not using the value of macro expansion.

Therefore the output of the program is square of j=9.

7) What will be the output of the below program?


1. #include<stdio.h>
2. #define IT 0.1
3. #define HRA 0.2
4. #define DA 0.3
5. int main()
6. {
7. float bas_sal, net_sal;
8. bas_sal=1000;
9. net_sal=bas_sal*(1+HRA+DA-IT);
10. printf("Gross salary=%f\n", net_sal);
11. return 0;
12. }
a. Gross salary=1000
b. Gross salary=1400
c. Compile error
d. No output

Hide Answer

The correct option is (b).

Explanation:

When the preprocessor sends the program for compilation IT, HRA and DA have been
replaced by 0.1, 0.2 and 0.3 respectively. With the bas_sal set to 1000, the net salary is
calculated as:

1. net_sal=bas_sal*(1+0.2+0.3-0.1);

The statement printf("Gross salary=%f\n", net_sal); prints the result. Therefore the
output of Output:

Gross salary=1400
8) It is necessary that header files must have the .h extension?
a. Yes
b. No

Hide Answer

The correct option is (b).

Explanation:

No, the header files may have any extension. It is not necessary that header file must have
.h extension.

9) Which header file is used for supporting the functions- malloc()


and calloc().
a. stdio.h
b. math.h
c. stdlib.h
d. memory.h

Show Answer

10) A macro statement can execute faster than function.


a. True
b. False

Hide Answer

The correct option is (a).

Explanation:

Macro statement can be executed faster with no overhead of context switch because the
code of macro gets expanded at the line of call.

Therefore the above statement is true.

11) Which operator is used for continue the definition of macro in


the next line?
a. $
b. #
c. ##
d. \

Hide Answer

The correct option is (d).

Explanation:

There is no such operator called $. The second and third operator are used for stringize and
token pasting operators respectively.

Therefore the operator '\' is used for continue the definition of macro in the next line.

12) Select the invalid predefined macro as per ANSI C.


a. __DATE__
b. __TIME__
c. __C++__
d. __FILE__

Hide Answer

The correct option is (c).

Explanation:

There is no such macro defined with the name __C++__, but the __cplusplus__ is
predefined macro as per ANSI C specification.

Therefore the __C++__ is invalid predefined macro as per ANSI C.

13) Find out the error in the below program?


1. #include<stdio.h>
2.
3. int main()
4. {
5. int j;
6. #if A
7. printf("Enter the number:");
8. scanf("%d", &j);
9. #elif B
10. printf("The number is even");
11. return 0;
12. }
a. The number is even
b. Error: unexpected end of file because there is no matching #endif
c. Garbage values
d. None of the above

Hide Answer

The correct option is (b).

Explanation:

The conditional macro statement #if must have an #endif statement. In the program there
is no #endif statement used.

Therefore the program returns an Error: unexpected end of file because there is no
matching #endif.

14) Which of the following are correct preprocessor directives in C


language?
1. #undef
2. #elif
3. #if
4. #ifdef

a. 1, 2, 4
b. 1, 3, 4
c. 1, 4
d. 1, 2, 3, 4

Hide Answer

The correct option is (d).

Explanation:

The macro statement #undef undefined the previously declared macro symbol.
The macro statement #if #ifdef #elif are called conditional macros.

Hence all the given statements are correct macro preprocessor directives in C language.

15) What will be the output of the below program on GCC


compiler?
1. #include <stdio.h>
2. #define MEAN(p, q, r, s, t) (p+q+r+s+t)/5
3. int main(){
4. int p=1, q=2, r=3, s=4, t=5, mn;
5. mn=MEAN(p,q,r,s,t);
6. printf("Mean of 5 numbers=%d\n",mn);
7. return 0;
8. }
a. Mean of 5 numbers=3
b. Mean of 5 numbers=5
c. Mean of 5 numbers=1
d. Compile error

Hide Answer

The correct option is (a).

Explanation:

In a program the macro MEAN is defined with an expansion.

During preprocessing, the occurrence of MEAN is replaced with their expansion, then send
to compiler and program is executed as below:

1. #include <stdio.h>
2. int main()
3. {
4. int p=1, q=2, r=3, s=4, t=5, mn;
5. m=(p+q+r+s+t)/5;
6. printf("Mean of 5 numbers=%d\n",mn);
7. return 0;
8. }

Therefore the output of the above program is "Mean of 5 numbers=3".

16) What will be the output of the below program?


1. #define MYFILE <stdio.h>
2. int main()
3. {
4. printf("Hello\n");
5. printf("Welcome to javatpoint\n");
6. return 0;
7. }
8. #include MYFILE
a. Hello\n Welcome to javatpoint\n
b. Hello Welcome to javatpoint
c. Hello Welcome to javatpoint
d. Compile error

Hide Answer

The correct option is (b).

Explanation:

o The name of a file can also be used as name of expansion.


o The preprocessor directive #include can be used anywhere in the program.

During preprocessing, the macro MYFILE gets replaced with <stdio.h>, then program is
compiled and executed as shown below:

1. int main()
2. {
3. printf("Hello\n");
4. printf("Welcome to javatpoint\n");
5. return 0;
6. }
7. #include <stdio.h>

Therefore the output of program is:

Hello

Welcome to javatpoint

17) Will it be result in to an error if a header file is included twice


in a program?
a. Yes
b. No
c. It is compiler dependent

Hide Answer

The correct option is (c).

Explanation:

In GCC compilers and Turbo C the compiler would take care of these problems and it
generates no error. In other compilers the error may occur.

Unless the header file has taken care for ensure that if already included then it doesn't get
included again.

Therefore an error in a program due to use of header file twice in a program is compiler
dependent.

18) The preprocessor can trap simple error like nested comments,
mismatch of braces or missing declarations.
a. True
b. False

Hide Answer

The correct option is (b).

Explanation:

The statement is false because the preprocessor cannot trap the errors; it only replaces the
macro with the given expression.

The compiler is used for detecting an error in a program.

19) In every C program there is at least one preprocessor


directive.
a. True
b. False

Hide Answer

The correct option is (b).


Explanation:

The statement is false because the preprocessor directive is not compulsory in any C
program. We can also develop a program in C language without using any preprocessor
directive.

20) What will be the output of the below program?


1. #include<stdio.h>
2. #define MAX(x, y) (x > y ? x : y)
3.
4. int main()
5. {
6. int a;
7. a = MAX(3+1, 2+4);
8. printf("%d\n", a);
9. return 0;
10. }
a. 5
b. 9
c. 8
d. 6

Hide Answer

The correct option is (d).

Explanation:

The macro MAX(x, y) (x > y ? x : y) returns the biggest value of the given two numbers.

Step 1: int a; The variable 'a' is declared as an integer type.

Step 2: a = MAX(3+1, 2+4); becomes,

=> a = (3+1 > 2+4 ? 3+1 : 2+4)

=> a = (4 > 6 ? 4 : 6)

=> a = 6

Step 3: printf("%d\n", a); It prints the value of variable 'a'.

Hence the output of the program is 6.

Vous aimerez peut-être aussi