This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1To calculate the square root of given value we use __________ predefined function in C language.
Option A
In C programming, when we want to calculate the square root of a number, we use a predefined function called sqrt(). This function is available in the math.h header file, so we must include it at the beginning of the program using #include<math.h>.
#include <stdio.h>
#include <math.h>
int main()
{
float num, result;
printf("Enter a Number: ");
scanf("%f", &num);
result = sqrt(num);
printf("Square Root of %f is %f",num,result);
return 0;
}
Q: 2Identify the format specifier used to print a number in octal representation.
Option C
In C language, different format specifiers are used to print numbers in various number systems.
The octal representation of a number is printed using the %o format specifier. For hexadecimal output, C provides two specifiers, %x for lowercase letters (a–f) and %X for uppercase letters (A–F). For decimal output %d or %i is used.
Q: 3The function scanf() returns?
Option D
The scanf() is a formatted input function used to take input from the user. Besides reading input, scanf() also returns a value.
The return value of scanf() is the number of input values that are successfully read and assigned to the given variables.
Q: 4Which of the following is formatted function?
Option C
Both printf() and scanf() are formatted input-output functions in C. The printf() function is used to display output on the screen using format specifiers while scanf() is used to read input from the user in a formatted manner using the format specifiers.
| FORMAT SPECIFIER | DATA TYPE |
|---|---|
| %c | For character type. |
| %s | For string. |
| %d | For signed integer. |
| %i | For signed integer. |
| %u | For unsigned integer. |
| %ld | For signed long integer. |
| %lu | For unsigned long integer. |
| %lld | For long long integer. |
| %p | For pointer (memory address). |
| %o | For octal number. |
| %x | For hexadecimal number. |
| %f | For float type. |
| %lf | For double type. |
| %% | For prints a percent sign. |
Q: 5How many main() functions can be defined in a C program?
Option B
In C, the execution of program begins from main() function. Therefore, a C program can have only one main() function. Defining more than one main() function will cause a compilation error because the compiler will not know where to start executing the program.
Q: 6The statement
char a=‘a’;
printf(“%d”,a);
Will print?
Option B
The character 'a' is stored in the variable a. When we print it using %d, C prints its ASCII value. The ASCII value of 'a' is 97.
Q: 7What is value of r, after executing the following code?
void main()
{
int a=10,b=20,r;
r=printf("%d %d",a,b);
printf("%d",r);
}
Option B
The printf() is not only used to display output, it also returns a value. The return value of printf() is the total number of characters printed on the screen.
The statement printf("%d %d", a, b); prints the value of a, then a newline, and then the value of b.
So, the total number of characters printed is 5.
This value 5 is returned by printf() and stored in the variable r. Then printf("%d", r); prints the value of r, which is 5.
Q: 8The fgets and gets functions are declared in the C header file __________.h
Option A
In C, both fgets() and gets() functions are used for input operations, specifically for reading strings from input. These functions are declared in the stdio.h (Standard Input-Output) header file.
Q: 9The unformatted input/output function in C language can work with?
Option B
Unformatted I/O functions do not use format specifiers and mainly work with single characters or strings. Example included getchar(), putchar(), gets(), puts(). These functions are simple and are mostly used for character-based input and output.
| FUNCTION | USED FOR | SYNTAX |
|---|---|---|
| getchar() | Reads a single character from keyboard. | ch=getchar(); |
| putchar() | Prints a single character on screen. | putchar(ch); |
| gets() | Reads a string. | gets(str); |
| puts() | Prints a string and moves to next line. | puts(str); |
Although gets() is unsafe because it does not check the size of input, which may cause buffer overflow and removed in modern C. The fgets() is used instead of gets() because it is safe and size-bounded.
Syntax:
fgets(string_name,size,stdin);
E.g.:
char name[50];
printf("Enter Name: ");
fgets(name,sizeof(name),stdin);
Q: 10Which of the following is example(s) of unformatted function in C language?
Option C
In C language, unformatted input/output functions are those functions that do not use format specifiers like %c, %d, %f, etc. and are mainly used for simple input or output, especially characters or strings. Commonly used unformatted I/O functions are getchar(), putchar(), gets(), puts().
Q: 11Which of the following functions provide the conversion symbol to identify the data type?
Option D
In C language, formatted input/output functions such as printf() and scanf() use conversion symbols (format specifiers) to identify the data type of the value being printed or read.
For example, the format specifier %d for integers, %f for floating-point values, %c for characters, and %s for strings.
| FORMAT SPECIFIER | DATA TYPE |
|---|---|
| %c | For character type. |
| %s | For string. |
| %d | For signed integer. |
| %i | For signed integer. |
| %u | For unsigned integer. |
| %ld | For signed long integer. |
| %lu | For unsigned long integer. |
| %lld | For long long integer. |
| %p | For pointer (memory address). |
| %o | For octal number. |
| %x | For hexadecimal number. |
| %f | For float type. |
| %lf | For double type. |
| %% | For prints a percent sign. |
Q: 12What value prints the below program
void main()
{
int a=65,b=065;
printf("%d %c",a,b);
}
Option B
In this program, the value of a is simply 65, which is a decimal number. But the value of b is written as 065, and in C, any number that begins with a leading zero is treated as an octal number.
The octal number 065 is converted to decimal as 53. When printing, %d displays the integer value 65, while %c prints the character that corresponds to ASCII value 53, which is the character '5'. Therefore, the output of the program becomes 65 5.
Q: 13What is value of r, after executing the following code?
void main()
{
int a,b,r;
r=scanf("%d%d",&a,&b);
printf("%d",r);
}
Option C
The scanf() is a formatted input function. The scanf() reads formatted input and returns the number of input values successfully read and assigned.
In the given code, scanf("%d%d", &a, &b); is used to read two integer values from the user, and when the user enters both integers correctly, scanf() successfully assigns the values to a and b.
In such a case, scanf() returns the number of inputs successfully read, which is 2. This return value is stored in the variable r, and finally printf("%d", r); prints the value of r, which is 2.
Q: 14The prototype of exit() function is defined in __________ header file.
Option A
The exit() function in C is used to terminate a program immediately. Its official and standard prototype is actually defined in stdlib.h.
However, the header file process.h where Turbo C/C++ provides the prototype of exit() as well.
Q: 15What value will print after executing the below program
void main()
{
int a,b;
a=65*66;
b='A'*'B';
printf("%d %d ",a,b);
}
Option B
In C language, characters are internally stored as integer values using their ASCII codes. Whenever arithmetic operations are performed on characters, their ASCII values are used automatically.
Here the variable a is assigned the value 65*66, which results in 4290. For the variable b, the characters 'A' and 'B' are used in an arithmetic operation. As we know, characters are internally represented by their ASCII values, where 'A' has an ASCII value of 65 and 'B' has an ASCII value of 66. Therefore, the expression 'A'*'B' is actually evaluated as 65*66, which also gives 4290.
Since the printf() statement uses the %d format specifier, both values are printed as integers.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.