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().
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.