0% found this document useful (0 votes)
109 views8 pages

"Hello": #Include

1. The document contains 20 sample questions and answers related to C programming. 2. The questions cover topics like loops, data types, operators, library functions, I/O functions, pointers, arrays, and more. 3. Each question is followed by a detailed explanation of the answer.

Uploaded by

fvdsd
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
109 views8 pages

"Hello": #Include

1. The document contains 20 sample questions and answers related to C programming. 2. The questions cover topics like loops, data types, operators, library functions, I/O functions, pointers, arrays, and more. 3. Each question is followed by a detailed explanation of the answer.

Uploaded by

fvdsd
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 8

SAMPLE QUESTIONS & ANSWERS - C PROGRAMMING

1 . What is the output of the below code snippet?

#include<stdio.h>
main()
{ for(;;)printf("Hello");}
A - Infinite loop
B - Prints “Hello” once.
C - No output
D - Compile error
Answer : A
Explanation
infinite loop, with second expression of ‘for’ being absent it is considered as true by default.

2 . What is the value of ‘y’ for the following code snippet?

#include<stdio.h>
main()
{
int x = 1;
float y = x>>2;
printf( "%f", y );
}
A-4
B - 0.5
C-0
D-1
Answer : C
Explanation
0, data bits are lost for the above shift operation hence the value is 0.

3. What is the output of the following program?

#include<stdio.h>
main()
{
int a[3] = {2,1};
printf("%d", a[a[1]]);
}
A-0
B-1
C-2
D-3
Answer : B
Explanation
1, The inner indirection evaluates to 1, and the value at index 1 for outer indirection is 1.

4 . What is the output of the following program?

#include<stdio.h>
main()
{
fprintf(stdout,"Hello, World!");
}
A - Hello, World!
B - No output
C - Compile error
D - Runtime error
Answer : C
Explanation
stdout is the identifier declared in the header file stdio.h, need to include the same.

5. What is the output of the following program?

#include<stdio.h>

main()
{
char s[20] = "Hello\0Hi";

printf("%d %d", strlen(s), sizeof(s));


}
A-59
B - 7 20
C - 5 20
D - 8 20
Answer : C
Explanation
Length of the string is count of character upto ‘\0’. sizeof – reports the size of the array.

6. In the standard library of C programming language, which of the following header file
is designed for basic mathematical operations?
A - math.h
B - conio.h
C - dos.h
D - stdio.h

Answer : A
Explanation
math.h is a header file in the standard library designed for basic mathematical operations

7.Which header file supports the functions - malloc() and calloc()?

A - stdlib.h
B - memory.h
C - math.h
D - stdio.h
Answer : A
Explanation
void *malloc(size_t size) : Allocates the requested memory and returns a pointer to it.

void *calloc(size_t nitems, size_t size): Allocates the requested memory and returns a pointer to
it.

8.What do you mean by “int (*ptr)[10]”

A - ptr is an array of pointers to 10 integers


B - ptr is a pointer to an array of 10 integers
C - ptr is an array of 10 integers
D - Invalid statement
Answer : B
Explanation: with or without the brackets surrounding the *p, still the declaration says it’s an
array of pointer to integers.

9.What will be the output of the following program?

#include<stdio.h>

int main()
{
const int i = 0;

printf("%d\n", i++);
return 0;
}
A - 100
B - Infinity
C-0
D - Return error
Answer : D
Explanation
It is because ++needs a value and a const variable can’t be modified.

#include<stdio.h>
int main()
{
const int i = 0;

printf("%d\n", i++);
return 0;
}
10.The library function strrchr() finds the first occurrence of a substring in another string.
A - Yes
B - Strstr()
C - strchr()
D - strnset()
Answer : B
Explanation
Strstr() finds the first occurrence of a substring in another string.

11.int x=~1; What is the value of 'x'?

A-1
B - -1
C-2
D - -2
Answer : D
Explanation
-2, the one’s compliment of 1 is 1110 (binary) which is equivalent to two’s compliment of 2, ie
-2.

12.What is the output of the following program?

#include<stdio.h>
void swap(int m, int n)
{
int x = m;

m = n;
n = x;
}
main()
{
int x=5, y=3;

swap(x,y);
printf("%d %d", x, y);
}
A-35
B-53
C-55
D - Compile error
Answer : B
Explanation
5 3, call by value mechanism can’t alter actual arguments.

#include <stdio.h>
void swap(int m, int n)
{
int x = m;

m = n;
n = x;
}
main()
{
int x=5, y=3;

swap(x,y);
printf("%d %d", x, y);
}
13. First operating system designed using C programming language.
A - DOS
B - Windows
C - UNIX
D - Mac
Answer : C
Explanation
UNIX. C actually invented to write an operation system called UNIX. By 1973 the entire UNIX
OS is designed using C.

14.What is the output of the following program?

#include<stdio.h>

main()
{
fprintf(stdout,"Hello, World!");
}
A - Hello, World!
B - No output
C - Compile error
D - Runtime error
Answer : C
Explanation
stdout is the identifier declared in the header file stdio.h, need to include the same.

15.What is the output of the following statement?

#include<stdio.h>

main()
{
printf("%d", -1<<1 );
}
A-2
B - -2
C-1
D - -1

Answer : B
Explanation
A negative number stored in two’s compliment of positive number. After shifting we get 1110,
which is equivalent to -2.

16.How do you specify double constant 3.14 as a long double?

A - By using LD after 3.14


B - By using L after 3.14
C - By using DL after 3.14
D - By using LF after 3.14
Answer : B
Explanation
The double constant 3.14 can be converted into long double by adding “L” after the constant
value, i.e; 3.14L.

17. “Stderr” is a standard error.


A - Yes
B - Standard error streams
C - Standard error types
D - Standard error function
Answer : B
Explanation
Standard error stream (Stderr) = Any program use it for error messages and diagnostics issue.

18.In DOS, What is the purpose of the function randomize() in Turbo C?


A - Displays a random number generator with a random value based on time
B - Displays a random number
C - Displays a random number generator in the specified range.
D - Invalid function
Answer : A
Explanation
randomize() picks the current time value as the SEED number to generate random numbers.

19.Choose the correct unary operators in C – a) !, b) ~, c) ^&, d) ++

A - a, b, d
B - a, b, c
C - b, c, d
D - c, d, a
Answer : A
Explanation
In C, these are the unary operators,

Logical NOT = !

Address-of = &

Cast Operator = ( )

Pointer dereference = *

Unary Plus = +

Increment = ++

Unary negation = –

20 - In the given below statement, what does the “arr” indicate?

char *arr[30];

A - arr is a array of function


B - arr is a array of 30 characters
C - arr is a pointer to an array
D - arr is a array of 30 character pointers
Answer : D
Explanation
square parenthesis signify as array at declaration and type is char*, so array of character
pointers.

You might also like