71% found this document useful [55 votes] 71K views 16 pages Multiple choice questions.. 100 TOP C Language Multiple
Choice Questions and Answers _ Multiple Choice Questions and Answers Beginners and Experienced PDF © © All Rights Reserved PDF, TXT or read online from ScribdDescription:
Original Title
Copyright
Available Formats
Share this document
Did you find this document useful?
71% found this document useful [55 votes]
71K views16 pages
100 TOP C Language Multiple Choice Questions and Answers - Multiple Choice Questions and Answers Beginners and Experienced PDF
Original Title:
100 TOP C Language Multiple Choice Questions and Answers _ Multiple Choice Questions and Answers Beginners and Experienced PDF
Jump to Page
You are on page 1of 16
You're Reading a Free Preview
Pages 6 to 14 are not shown in this preview.
Reward Your Curiosity
Everything you want to read.
Anytime. Anywhere. Any device.
No Commitment. Cancel anytime.
Latest Programming in C MCQ Objective Questions
Programming in C MCQ Question 1:
Consider the following C code given below:
void func1[char a[], char b[]] {
int i, j;
i = j = 0;
while [a[i] != '\0']
i++;
while [[a[i++] = b[j++]] != '\0'];
}
Let the string a = "1010\0100" and b = "0101011\0011". What will be the number of of characters in string a after the above function is called?
Answer [Detailed Solution Below] 11
Explanation:
- void func1[char a[], char b[]]
{
A function func1[] is called with a and b passed as string parameters.
- int i, j;
Two integer variables [i, j] will be declared and they will be used as the indices to the characters of their respective strings.
- i = j = 0;
The indices are initialised with 0 so that they can be used to point the first character of both the respective strings.
- while [a[i] != '\0'] i++;
With this line, one by one the index i will increase by 1 till the NULL character [\0] is found it will stop incrementing. Hence, after this statement executes, value of i will be 4. Any characters further will not be counted because the while condition became false and the flow is out of the while loop.
- while
[[a[i++] = b[j++]] != '\0'];
Now, with this statement, i = 4 and j = 0. Till b[j] encounters NULL character, b[j] will be stored in a[i] and both i and j will be incremented by 1. Hence, a becomes 10100101011. The main thing to notice here is that it will not store the NULL character of the string b in a. Hence, total 11 characters will be printed.
Programming in C MCQ Question 2:
Determine the size of the memory allocation for an object of a class.
class Exam_marks
{
int math, eng;
float cal_avg;
char subject[10];
};
- 10
- 17
- 22
- 28
Answer [Detailed Solution Below]
Option 3 : 22
The correct answer is 22.
line 1- class Exam_marks: Defining the class named 'Exam_marks'.
line 2-int math, eng; : Under the class math and eng are integer type variables.
line 3-float cal_avg; : The variable cal_avg is a floating type variable used for calculating the average.
line 4-char subject[10]; : An array variable subject[10] is defined as a character type with a size 10.
Now to determine the size of an object, the programmer needs to sum up all the sizes of the data members of the class, except static members.
Here, we will get, int= 4 bytes, float =4 bytes, char = 1 byte,
So, the total size will be= [2x int+1x float+ 10x char]bytes =[2x4+ 4x1 +10x1] bytes = 22 bytes.
Thus the correct answer is 22.
- The smallest size of memory is counted as bits. 8 bits is equal to 1 byte.
- There are different data types used for storing data in the computer system. integer type, floating type, character type etc.
- The size of the integer data type is 4 bytes. The long data type holds 8 bytes.
- The float is 4 bytes. The double is 8 bytes and the long double is 10 bytes.
- The size char data type is 1 byte.
Programming in C MCQ Question 3:
After occupying the memory by calloc[], what function should be used to free the memory?
- dealloc[]
- malloc[variable,0]
- free[]
- demalloc[variable,0]
Answer [Detailed Solution Below]
Option 3 : free[]
The correct answer is free[].
- free[]: The function free[] is deallocating the memory which is occupied by the calloc[] function during the program.
- The syntax is given below,
void free[ void *ptr]
- Here, ptr is defined as a pointer to a memory block that is occupied by the calloc[] function during the program.
- The free[] does not return any value to the program.
Thus the correct answer is free[].
- In the C programming language, when a procedure used for changing the size of a data structure during the runtime is known as Dynamic Memory Allocation.
- The C programming language provides some functions to achieve these tasks.
- Four library functions are handed by C defined under to accelerate dynamic allocation in C programming. These are malloc[]. calloc[], free[], realloc[].
- The calloc[0 functions are used for allocating contiguous memory allocation.
Programming in C MCQ Question 4:
In which order recursive functions are executed?
- parallel order
- FIFO
- LIFO
- Iterative order
Answer [Detailed Solution Below]
Option 3 : LIFO
The correct answer is LIFO.
The recursive function is utilized for the capability that is alluded to as execution of itself. It is utilized for addressing any errand that is available.
The function of recursion has contentions that will make an errand so natural and capability won't settle on decisions further.
In a program, a function is made because the stack outline is known as Active Record Instance. Then, at that point, the capability call of every passage will be made.
Thus, it tends to be reasoned that it will be executed as LIFO which is the Last In First Out Order.
Thus the correct answer is LIFO.
- A capability of a function that calls itself is known as a recursive function. Also, this strategy is known as recursion.
- Until achieves to the goal of a program, the recursion function keeps going on.
- The recursion functions are mainly used in data structure and algorithms. Tree traversal is a good example where the recursion function is used.
Programming in C MCQ Question 5:
What will be the output of the program?
void main[]
{
printf[“%p”, main];
}
- generating errors
- generating infinite loop
- print some addresses
- None of the above
Answer [Detailed Solution Below]
Option 3 : print some addresses
The correct answer is print some addresses.
- line 1: void main[] - It is the main function of a program that uses some addresses.
- line 2: printf["%p", main] - The printf function prints the output. The format specifier "%p" was used for printing the pointer data type. It will print the address of the main function.
- The pointer holds the addresses and the format specifier "%p" will print the address.
Thus the correct answer is print some addresses.
- The function runs a set of instructions or codes only when it is called in the program.
- A set of parameters, data can pass through a function.
- A function can be called multiple times in the main function or any other function.
Top Programming in C MCQ Objective Questions
Which programming language is called the mother of programming languages?
- C++
- C
- Java
- COBOL
Answer [Detailed Solution Below]
Option 2 : C
The Correct Answer is Option [2] i.e C.
- The C language is also known as the mother of all programming languages.
- C is a general-purpose programming language that is used for creating a variety of applications.
- C language was originally developed for writing operating systems. Unix Kernel and all of its supporting tools and libraries are written in C language.
- The C language is used for the following operations :
- Operating systems
- Development of new languages
- Computation platforms
- Embedded systems
- Graphics and Games
- C++ and Java are the high-level languages and COBOL is a compiled English-like computer programming language.
Consider the following C declaration
struct {
short s[5];
union {
float y;
long z;
}u;
}t;
Assume that objects of type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement for variable t, ignoring alignment considerations, is
- 22 bytes
- 18 bytes
- 14 bytes
- 10 bytes
Answer [Detailed Solution Below]
Option 2 : 18 bytes
The correct answer is "option 2"
CONCEPT:
Structure in C is a user-defined data type that is used to store the collection of different data types.
The total size of the structure is the sum of the size of every data member.
Union is a user-defined data type that is used to store different data types in the same memory location.
The total size of the union is the size of the largest data member.
EXPLANATION:
Given the size of short, float and long is 2 bytes, 4 bytes, and 8 bytes, respectively.
Therefore,
Size of Structure → size of [ short s[5] ] + size of Union
Here, Size of Union = 8 bytes { largest data member is long }.
Size of short s[5] → 2×5 → 10 bytes
Size of Structure → 10+8 →18 bytes.
Hence, the correct answer is "option 2".
Consider the following C program:
#include
int main [ ]
{
int a[ ] = {2, 4, 6, 8, 10};
int i, sum = 0, *b = a + 4;
for [i = 0; i < 5; i++]
sum = sum + [*b - i] - *[b - i];
printf [“ % d \ n”, sum];
return 0;
}
The output of the above C program is ________.
Answer [Detailed Solution Below] 10
integer pointer b points to 5th element of an static array a
*b = 10 // dereferencing
sum = 0 // initially
Values in loop | sum = sum + [*b - i] - *[b - i]; |
i = 0 | sum = 0 + [10 - 0] - *[address_of_5th integer - 0] = 10 - *[address_of_5th integer] = 10 - 10 = 0 |
i = 1 | sum = 0 + [10 - 1] - *[address_of_5th integer - 1] = 0 + [10 - 1] - *[address_of_4th integer] = 9 - 8 = 1 |
i = 2 | sum = 1 + [10 - 2] - *[address_of_5th integer - 2] = 1 + [10 - 2] - *[address_of_3rd integer] = 9 - 6 = 3 |
i = 3 | sum = 3 + [10 - 3] - *[address_of_5th integer - 3] = 3 + [10 - 3] - *[address_of_2nd integer] = 10 - 4 = 6 |
i = 4 | sum = 6 + [10 - 4] - *[address_of_5th integer - 4] = 6 + [10 - 4] - *[address_of_1st integer] = 12 - 2 = 10 |
Which of the following viruses codifies itself in an automatic manner, each time it infects and copies the system.
- Polymorphic virus
- File Allocation Table [FAT] Virus
- Overwrite viruses
- Multipartite virus
Answer [Detailed Solution Below]
Option 1 : Polymorphic virus
The correct answer is Polymorphic virus.
- A polymorphic virus is a complicated computer virus that affects data types and functions.
- Polymorphic viruses are complex file infectors that can create modified versions of themselves to avoid detection yet retain the same basic routines after every infection.
- Upon infection, the polymorphic virus duplicates itself by creating usable, albeit slightly modified, copies of itself.
- To vary their physical file makeup during each infection, polymorphic viruses encrypt their codes and use different encryption keys every time.
- It is a self-encrypted virus designed to avoid detection by a scanner.
Consider the following array declaration in ‘C’ language:
int array[] = {2, 3, 4, 5};
What will be the output of the following statement?
printf["%d", 2[array]];
- 4
- 3
- 2
- 5
Answer [Detailed Solution Below]
Option 1 : 4
An array is defined as the collection of similar types of data items stored at contiguous memory locations. Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. C array is beneficial if you have to store similar elements.
int array[] = {2, 3, 4, 5}; This array storage be like,
The above array at index I can be accessed by, a[i], i[a], *[a+i] or *[i+a] all the above representations all are equal and gives the i th index value.
printf[%d', 2[array]]; So, it gives the 2nd index value. i.e 4.
Hence the correct answer is 4.
#include
int main[]
{
int arr[] = { 2, 3, 4, 5 };
printf["%d ",arr[2]];
printf["%d ",2[arr]];
printf["%d ",*[2+arr]];
printf["%d ",*[arr+2]];
return 0;
}
Output: 4 4 4 4
So above given 2nd index value is 4 for all above print statements.
Which one of the following is not an example of computer language?
- ALGORITHM
- FORTRAN
- PASCAL
- COBOL
Answer [Detailed Solution Below]
Option 1 : ALGORITHM
The correct answer is ALGORITHM.
- An algorithm is a specific procedure for solving a well-defined computational problem.
- The development and analysis of algorithms are fundamental to all aspects of Computer Science like AI, databases, graphics, networking, OS, etc.
- Algorithm development is more than just programming.
- It requires an understanding of the alternatives available for solving a computational problem.
- It includes the hardware, networking, programming language, and performance constraints that accompany any particular solution.
- An official language comprising a collection of instructions that produce different kinds of output is a programming language.
- In computer science, programming languages are used to implement algorithms.
- Most of the languages of programming consist of computer instructions.
Pascal |
|
Fortron |
|
Cobol |
|
Considering the size of char [character] variables as one byte, what will be the size of the array declared below?
char array[ ] = “programming language”;
- 11 Bytes
- 8 Bytes
- 20 Bytes
- 21 Bytes
Answer [Detailed Solution Below]
Option 4 : 21 Bytes
Each character is of one byte:
Byte number and corresponding bytes are given for character array
1st | 2nd | 3rd | 4th | 5th | 6th | 7th | 8th | 9th | 10th | 11th | 12th | 13th |
p | r | o | g | r | a | m | m | i | n | g | l |
14th | 15th | 16th | 17th | 18th | 19th | 20th | 21st |
a | n | g | u | a | g | e | \0 |
Therefore, the size of the array declared is 21 bytes
Important Point:
\0 is null character.
What is the output in a 32 bit machine with 32 bit compiler?
#include
rer[int **ptr2,int **ptr1]
{
int*ii;
ii=*ptr2;
*ptr2=*ptr1;
*ptr1=ii;
**ptr1 *= **ptr2;
**ptr2 += **ptr1;
}
void main[ ]
{
int var1=5, var2=10;
int *ptr1=&var1, *ptr2=&var2;
rer[&ptr1, &ptr2];
printf[“%d %d “,var2, var1];
}
- 60 70
- 50 50
- 50 60
- 60 50
Answer [Detailed Solution Below]
Option 4 : 60 50
Concept –
void main[] | Suppose var1 address 1000 | var2 address 2000 |
int var1=5, var2=10; | var1 = 5 | var2 = 10 |
int *ptr1=&var1, *ptr2=&var2; | ptr1 = 1000 [pointer storing address of var1] | ptr2 = 2000 [pointer storing address of var2] |
rer[&ptr1,, &ptr2]; | Call to the function describes in table below |
rer[int **ptr2,int **ptr1] | ii | ptr2 | ptr1 | var1 | var2 |
int* ii; | - | 1000 | 2000 | 5 | 10 |
ii = *ptr2; | 1000 | 1000 | 2000 | 5 | 10 |
*ptr2 = *ptr1; | 2000 | 2000 | 2000 | 5 | 10 |
*ptr1 = ii; | 2000 | 2000 | 1000 | 5 | 10 |
**ptr1 *= **ptr2 | 2000 | 2000 | 1000 | 5× 10 = 50 | 10 |
**ptr2 += **ptr1; | 2000 | 2000 | 1000 | 50 | 60 |
Hence, printf[“%d %d “,var2, var1]; will print 60 and 50.
Consider the following C program.
# include
struct Ournode {
char x, y, z ;
} ;
int main [ ] {
struct Ournode p = {‘1’, ‘0’, ‘a’ + 2} ;
struct Ournode *q = &p;
printf [“%c, %c”, *[ [char*] q + 1] , *[ [char*] q + 2] ] ;
return 0 ;
}
The output of this program is:
- 0, c
- 0, a + 2
- ‘0’, ‘a + 2’
- ‘0’, ‘c’
Answer [Detailed Solution Below]
Option 1 : 0, c
Character ‘a’ has ASCII value 97, adding 2 will result in 99, which is the ASCII value for ‘c’.
Hence dereferencing q + 1 and q + 2 will give 0 and c respectively.
char x = 'a' + 2, that is, x == 'c'
So, p={'1','0','c'};
*[[char*]q+1] = *[address of data '1' + 1] = *[address of data '0'] = 0;
*[[char*]q + 2] = *[address of data '1' + 2] = *[address of data 'c'] = c;
printf["%c, %c",*[[char*]q+1],*[[char*]q+2]] will print 0, c.
Consider the following C program.
#include
void mystery [int *ptra, int *ptrb] {
int *temp;
temp = ptrb;
ptrb = ptra;
ptra = temp;
}
int main [] {
int a=2016, b=0, c=4, d=42;
mystery [&a, &b];
if [a < c]
mystery [&c, &a];
mystery [&a, &d] ;
print ["%d\n", a];
}
The output of the program is __________.
Answer [Detailed Solution Below] 2016
In the given program, in mystery function only addresses are swapped not the values. Values remain same as in the main function. Pointer swapping is local to the mystery function here.
In C language, parameters are passed by value even if they are pointer. So, there will be no change in the value of a, b, c and d.
Code Explanation:
int main [] {
int a = 2016, b = 0, c = 4, d = 42; // a= 2016, b= 0
mystery [&a, &b]; // call void mysery [int *ptra, int *ptrb]
if [a < c]
mystery [&c, &a];
mystery [&a, &d];
printf [‘’%d\n’’, a];
}
void mysery [int *ptra, int *ptrb] // ptra = 2016, ptrb =0 during first call
{
int *temp;
temp = ptrb;
ptrb = ptra;
ptra = temp; // ptra = 0, ptrb = 2016,
}
But after this mystery[&a, &b]; popped out of stack and all the pointers lost and all values wil remain same as original.
printf [‘’%d\n’’, a]; // it will print the value of a i.e. 2016.
Which of the following programming languages is mainly popular for business data processing?
- C
- Pascal
- FORTRAN
- COBOL
Answer [Detailed Solution Below]
Option 4 : COBOL
The correct answer COBOL.
- COBOL stands for Common Business Oriented Language.
- It was developed by the US Department of Defense as a language for business data processing needs.
What is the value of the following 'C' language expression in which x is an integer variable whose value is 4?
[x - 5] ? 15: 25
- 0
- 25
- 15
- -1
Answer [Detailed Solution Below]
Option 3 : 15
The conditional operator is kind of similar to the if-else statement as it does follow the same algorithm as of if-else statement but the conditional operator takes less space and helps to write the if-else statements in the shortest way possible.
Syntax:
The conditional operator is of the form,
variable = Expression1? Expression2: Expression3;
It can be visualized into the if-else statement as:
if[Expression1]
{
variable = Expression2;
}
else
{
variable = Expression3;
}
Given that,
[x - 5] ? 15 : 25;
here x=4, [4-5]= -1 and take it as true and print 15 as output.
If x=6, [6-5]= 1 and take it as true and print 15 as output.
If x=5, [5-5]= 0 and take it as false and print 25 as output.
Note:
Negative values, and any non-zero values in general, are treated as true when used as conditions. For C, there are a number of contexts in which an expression is treated as a condition.
Hence the correct answer is15.
What will be the output of the following C code?
#include
main[ ]
{
int i;
for [ i=0; i