C++ Multiple Choice questions and Answers PDF
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 Scribd 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 PDFOriginal 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 Reward Your CuriosityEverything you want to read. Anytime. Anywhere. Any device. No Commitment. Cancel anytime. Latest Programming in C MCQ Objective QuestionsProgramming 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) 11Explanation:
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]; };
Answer (Detailed Solution Below)Option 3 : 22 The correct answer is 22. Key Points
Thus the correct answer is 22. Additional Information
Programming in C MCQ Question 3:After occupying the memory by calloc(), what function should be used to free the memory?
Answer (Detailed Solution Below)Option 3 : free() The correct answer is free(). Key Points
void free( void *ptr)
Thus the correct answer is free(). Additional Information
Programming in C MCQ Question 4:In which order recursive functions are executed?
Answer (Detailed Solution Below)Option 3 : LIFO The correct answer is LIFO. Key Points
Thus the correct answer is LIFO. Additional Information
Programming in C MCQ Question 5:What will be the output of the program? void main() { printf(“%p”, main); }
Answer (Detailed Solution Below)Option 3 : print some addresses The correct answer is print some addresses. Key Points
Thus the correct answer is print some addresses. Additional Information
Top Programming in C MCQ Objective QuestionsWhich programming language is called the mother of programming languages?
Answer (Detailed Solution Below)Option 2 : C The Correct Answer is Option (2) i.e C.
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
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) 10integer pointer b points to 5th element of an static array a *b = 10 // dereferencing sum = 0 // initially
Which of the following viruses codifies itself in an automatic manner, each time it infects and copies the system.
Answer (Detailed Solution Below)Option 1 : Polymorphic virus The correct answer is Polymorphic virus. Key Points
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]);
Answer (Detailed Solution Below)Option 1 : 4 Key Points 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. Additional InformationProgram: #include 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?
Answer (Detailed Solution Below)Option 1 : ALGORITHM The correct answer is ALGORITHM. Key Points
Additional Information
Considering the size of char (character) variables as one byte, what will be the size of the array declared below? char array[ ] = “programming language”;
Answer (Detailed Solution Below)Option 4 : 21 Bytes Each character is of one byte: Byte number and corresponding bytes are given for character array
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); }
Answer (Detailed Solution Below)Option 4 : 60 50 Concept –
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:
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) 2016In 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?
Answer (Detailed Solution Below)Option 4 : COBOL The correct answer COBOL.
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
Answer (Detailed Solution Below)Option 3 : 15 Key Points 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<5; i++ ) { int i = 10; printf("%d", i); i++; } return 0; }
Answer (Detailed Solution Below)Option 2 : 10 10 10 10 10 #include main( ) { int i; for ( i=0; i<5; i++ ) //this loop runs 5 times { int i = 10;// initialized to 10 printf("%d", i); // It prints 10 i++;// increamented by 1 but here i is a local variable inside a for loop.[this i values are 0,1,2,3,4] } return 0; } Output will be 10 10 10 10 10 What is the size of the following array declaration in 'C, if character requires one-byte memory space? char name[]="abcde";
Answer (Detailed Solution Below)Option 2 : 6 bytes Key Points char name[]="abcde"; it is a character string definition. The string is a sequence of characters or an array of characters. The declaration and definition of the string using an array of chars is similar to declaration and definition of an array of any other data type. The constructor of the string class will set it to the C-style string, which ends at the ‘\0’. size of above string "abcde" is =6 bytes (5 bytes + 1 byte of \0 ) Hence the correct answer is 6 bytes. Consider the following C program. #include int main ( ) { static int a [ ] = {10, 20, 30, 40, 50} ; static int *p [ ] = {a, a+3, a+4, a+1, a+2} ; int **ptr = p; ptr++; printf(“%d%d”, ptr-p, **ptr) ; The output of the program is _________ Answer (Detailed Solution Below) 140Concept: Rule of pointer arithmetic: When two pointers are subtracted belonging to same array then result is number of elements separating them. Explanation: Consider integer size as 2 bytes. Consider base address of array as 100. Let array p base address is 200 and double ptr base address is 300 ptr++; // It points to address 202 ptr – p \(\frac{{address\;of\;ptr\; - \;address\;of\;p}}{{size\;of\;int}}\; = \frac{{202 - 200}}{2} = 1\;\) ** ptr = ** 202 = * (a+3) = 40 printf(“%d%d”, ptr – p, **ptr) ; It will print 140 Consider the following C program: #include int main ( ) { int arr [ ] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 5}, *ip = arr + 4; printf (“ % d \n”, ip[1]); return 0; } The number that will be displayed on execution of the program is _________. Answer (Detailed Solution Below) 6int *ip = arr + 4 //ip is a integer pointer pointing to 5 element of the array ip[1] = *(ip + 1xsizeofint) = *(address of 6th element) = 6 Consider the following C function #include char c[] = "ICRBCSIT17"; The output of the program is
Answer (Detailed Solution Below)Option 4 : 17 Here, character array c will be stored to *p.
c = 100 (starting address of array) 2[p] = p[2] = R 6[p] = p[6] = I c + 2[p] - 6[p] - 1 = c + R - I - 1 ASCII value of A is 65. ASCII of R is 82 and ASCII value of I is 73 = 100 + 82 - 73 - 1 = 100 + 8 =108 Hence printf("%s", 108); will print 17 because %s will get all the values untill it gets NULL. Match the following list:
Answer (Detailed Solution Below)Option 4 : (a) – (iii), (b) – (i), (c) – (iv), (d) – (ii) Concept: Storage classes are used to fully define a variable. Storage class tells us that where the variable would be stored, what will be initial value of variable (if initial value is not specifically assigned then default value), what would be the scope of variable and life of variable. Explanation: Four types of storage classes: auto, register, static and extern. auto: Features of auto storage class are :
Features of register storage class are:
Features of static storage class are:
Features of extern storage class are:
If, X, Y and Z are pointer variables of type char, int and float, respectively in 'C' language, then which of the following statements is true?
Answer (Detailed Solution Below)Option 1 : Size of X, Y, and Z are same Key Points Given that, char * x; int * y; float * z; Then, Size of x is= 2 Bytes Size of y is= 2 Bytes Size of z is= 2 Bytes Important Point
Hence here all pointer sizes are the same. Hence the correct answer is Size of X, Y, and Z are the same. What is the output of the following C code? Assume that the address of x is 2000 (in decimal) and an integer requires four bytes of memory. int main () { unsigned int x[4][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}; printf(“%u, %u, %u”, x + 3, *(x + 3), *(x + 2) + 3); }
Answer (Detailed Solution Below)Option 1 : 2036, 2036, 2036 x is a 2-D array of arrays |