C Important Question
C Important Question
5) C Strings MCQs
9. A string is terminated by ___.
A. Newline ('\n')
B. Null ('\0')
C. Whitespace
D. None of the above
Explanation:
10. Which format specifier is used to read and print the string using printf() and
scanf() in C?
A. %c
B. %str
C. %p
D. %s
ANSWER: D) %s
Explanation:
The format specifier "%s" is used to read and print the string using printf() and scanf() in
C.
Example:
#include <stdio.h>
int main()
{
char name[30];
return 0;
}
/*
Output:
Input name: Alvin
Given name is: Alvin
*/
11. Which function is used to read a line of text including spaces from the user in
C?
A. scanf()
B. getc()
C. fgets()
D. All of the above
ANSWER: C) fgets()
Explanation:
In C programming language, the fgets() function can be used to read a line of text
including spaces from the user.
Syntax:
A. concat()
B. cat()
C. stringcat()
D. strcat()
ANSWER: D) strcat()
Explanation:
Syntax:
strcat(s1, s2);
#include <stdio.h>
int main()
{
char str1[] = { 'H', 'e', 'l', 'l', 'o' };
char str2[] = "Hello";
return 0;
}
A. 5,5
B. 6,6
C. 5,6
D. None of the above
ANSWER: C) 5,6
Explanation:
str1 is initialized with the characters and there are only 5 characters. Thus, the length of
the str is 5. While, str2 is initialized with the string "Hello", when we initialized the string
in this way - a null ('\0') character is inserted after the string. Thus, the length of str2 is 6.
int main()
{
char str1[] = "Hello";
char str2[10];
str2 = str1;
return 0;
}
A. Hello,
B. Hello,Hello
C. Hello,HelloHello
D. Error
ANSWER: D) Error
Explanation:
There will be a compilation error, because we cannot assign a string like this (str2 = str1).
To resolve this issue, we have to use strcpy(str2,str1).
15. What will be the output of the following C code? (If the input is "Hello world")
#include <stdio.h>
int main()
{
char str[30];
scanf("%s", str);
printf("%s", str);
return 0;
}
A. Hello world
B. Hello
C. Hello world\0
D. Error
ANSWER: B) Hello
Explanation:
When we read a string using the scanf() function, the input is terminated by the
whitespace. Here, the input is "Hello world", so only "Hello" will be stored to str. Thus, the
output will be "Hello".
16. Which function is used to compare two strings in C?
A. strcmp()
B. strcmpi()
C. compare()
D. cmpi()
ANSWER: A) strcmp()
Explanation:
The strcmp() is a built-in library function and is declared in <string.h> header file. This
function takes two strings as arguments and compare these two strings lexicographically.
Syntax:
17. Which function is used to compare two strings with ignoring case in C?
A. strcmp()
B. strcmpi()
C. compare()
D. cmpi()
ANSWER: B) strcmpi()
Explanation:
The function strcmpi() is used to compare two strings with ignoring case in C language.
The strcmpi() is a built-in library function and is declared in <string.h> header file. This
function takes two strings as arguments and compare these two strings, it is similar
to strcmp() function but the only difference is that strcmpi() function is not case sensitive.
Syntax:
6) C Arrays MCQs
18. Which is the correct syntax to declare an array in C?
A. data_type array_name[array_size];
B. data_type array_name{array_size};
C. data_type array_name[];
D. All of the above
data_type array_name[array_size];
A. values
B. indices
C. memory addresses
D. All of the above
ANSWER: B) indices
Explanation:
You can access elements of an array by indices. Array indices starts from 0 to array_size -1.
Explanation:
Example:
A. Random
B. Sequential
C. Both A and B
D. None of the above
ANSWER: B) Sequential
Explanation:
printf("%u",x);
ANSWER: D) Print the address of the array (i.e., the address of first (0th) element
Explanation:
If we print the array (in this case, x). The output will be the memory address of the array
(i.e., the address of first (0th) element.
#include <stdio.h>
int main()
{
int x[5] = { 10, 20, 30 };
printf("%d", x[3]);
return 0;
}
A. 0
B. 30
C. Garbage value
D. Error
ANSWER: A) 0
Explanation:
In C language, when an array is partially initialized at the time of declaration then the
remaining elements of the array is initialized to 0 by default.
#include <stdio.h>
int main()
{
int x[5] = { 10, 20, 30 };
printf("%d", x[-1]);
return 0;
}
A. 0
B. 10
C. Garbage value
D. Error
Explanation:
C language compiler does not check array with its bounds, when an index is out of the
range the garbage value is printed.
#include <stdio.h>
int main()
{
int x[5] = { 10, 20, 30 };
printf("%ld", sizeof(x)/sizeof(x[0]));
return 0;
}
A. 3
B. 4
C. 5
D. 6
ANSWER: C) 5
Explanation:
The statement sizeof(x)/sizeof(x[0]) can be used to get the total numbers of elements,
sizeof(x) will return the size of array while sizeof(x[0]) will return the size of first element
i.e., size of the type. Their division will return the total number of elements
#include <stdio.h>
int main()
{
int x[5] = { 10, 20, 30 };
printf("%d", x[3]);
return 0;
}
A. 0
B. 30
C. Garbage value
D. Error
ANSWER: A) 0
Explanation:
In C language, when an array is partially initialized at the time of declaration then the
remaining elements of the array is initialized to 0 by default.
#include <stdio.h>
int main()
{
int x[5] = { 10, 20, 30 };
printf("%d", x[-1]);
return 0;
}
A. 0
B. 10
C. Garbage value
D. Error
Explanation:
C language compiler does not check array with its bounds, when an index is out of the
range the garbage value is printed.
ANSWER: C) Base address of the array i.e., the address of the first element
Explanation:
If we pass an array as an argument to a function – the base address of the array (the
address of the first element) is passed to the function
A. structure
B. string
C. array
D. All of the above
ANSWER: A) structure
Explanation:
In C programming language, a structure is a user-defined data type which is the
collection of different data types.
A. -
B. >
C. *
D. .
ANSWER: D) .
Explanation:
A. int
B. union
C. char
D. All of these
ANSWER: B) union
Explanation:
32. "A union can contain data of different data types". True or False?
A. True
B. False
ANSWER: A) True
Explanation:
Union is a user defined data structure which can store data variables of different data
types.
A. un
B. union
C. Union
D. None of these
ANSWER: B) union
Explanation:
Explanation:
The size of the union is equal to the size of the largest data type declared in the union.
Explanation:
Explanation:
Unions in C is a data structure which store data types but not the methods.
Explanation:
A. union union_name {
};
B. union union_name {
}
C. union union_name (
);
D. Union union_name (
)
ANSWER: A)
union union_name {
};
Explanation:
union union_name {
};
#include <stdio.h>
union values {
int val1;
char val2;
} myVal;
int main()
{
myVal.val1 = 66;
return 0;
}
A. val1 = 0x54ac88dd2012
val2 = 0x55ac76dd2014
B. Error
C. val1 = 0x55ac76dd2014
val2 = 0x55ac76dd2014
D. Exception
Explanation:
A. True
B. False
ANSWER: A) True
Explanation:
A. Return
B. Value
C. Return type
D. All of these
Explanation:
A. Call by value
B. Call by reference
C. Call by pointer
D. All of these
Explanation:
Explanation:
Explanation:
A. Current block
B. Only function
C. Whole file
D. Directory
Explanation:
48. Which of the below syntax is the correct way of declaring a function?
A. return function_name () {
}
B. data_type function_name (parameter) {
}
C. Void function_name (
)
D. None of these
ANSWER: B)
Explanation:
A. Square
B. Square of reverse bits
C. Square root
D. None of these
Explanation:
#include <stdio.h>
int main(){
int a = myFunc(13);
printf("%d", a);
return 0;
}
A. 13
B. 12
C. 14
D. Error
ANSWER: B) 12
51. The meaning of keyword void before the function name means________.
A. function should not return any value.
B. function should return a value.
C. no arguments are passed.
D. some arguments are passed
ANSWER: A
===============================================
C Functions-1
53. Choose correct statement about Functions in C Language.
a) A Function is a group of c statements which can be reused any number of times
b) Every Function has a return type
c) Every Function may no may not return a value
d) All the above
ANSWER: D
No explanation is given for this question.
ANSWER: D
No explanation is given for this question.
ANSWER: C
No explanation is given for this question.
56. The keyword used to transfer control from a function back to the calling function is
int **a;
a) switch
b) goto
c) go back
d) return
ANSWER: D
The keyword return is used to transfer control from a function back to the calling function.
int main() {
printf("Algbly");
main();
return 0;
}
a) Infinite times
b) 32767 times
c) 65535 times
d) Till stack overflows
ANSWER: D
A call stack or function stack is used for several related purposes, but the main reason for
having one is to keep track of the point to which each active subroutine should return control
when it finishes executing.
A stack overflow occurs when too much memory is used on the call stack.
Here function main() is called repeatedly and its return address is stored in the stack. After
stack memory is full. It shows stack overflow error.
void main() {
printf("CACHEW ");
return 10;
}
a) PISTA CACHEW
b) CASHEW PISTA
c) PISTA CASHEW with compiler warning
d) Compiler error
ANSWER: C
Here show() function should not return anything. So, return 10; is not recommended.
ANSWER: C
No explanation is given for this question.
ANSWER: D
No explanation is given for this question.
ANSWER: D
There is no limit on the number of functions.
62. Every C Program should contain which functionEvery C Program should contain which function?
a) printf()
b) show()
c) scanf()
d) main()
ANSWER: D
main() is a compulsory function with or without returning anything.
========================================================
C Arrays and Strings-1
63. Which function will you choose to join two words?
a) strcpy()
b) strcat()
c) strncon()
d) memcon()
ANSWER: B
The strcat() function is used for concatenating two strings, appends a copy of the string.
ANSWER: A
The memcpy() function is used to copy n characters from the object.
ANSWER: C
The strncat() function appends not more than n characters from the array.
ANSWER: D
The strcmp() function compares the string s1 to the string s2.
ANSWER: B
No explaination is given for that question.
ANSWER: A
It is a simple way of creating a C String. You can also define it like the below. \0 is mandatory in
this version.
ANSWER: C
No explaination is given for this question.
a) D
b) Discovery Channel
c) Discovery
d) Compiler error
ANSWER: B
%s prints the while character array in one go.
a) g
b) globe
c) globe\0
d) None of the above
ANSWER: D
Notice that you have not added the last character \0 in the char array. So it is not a string. It can
not know the end of string. So it may print string with some garbage values at the end.
71. What's wrong in the following statement, provided k is a variable of type int?
int main() {
char str[] = {'g', 'l', 'o', 'b', 'a', 'l','\0'};
printf("%s", str);
return 0;
}
a) g
b) globe
c) globe\0
d) Compiler error
ANSWER: B
Adding a NULL or \0 at the end is a correct way of representing a C string. You can simple use
char str[]="globy". It is same as above.
================================================================
C Structures-1
a) 5
b) 11
c) 41
d) 44
ANSWER: D
No explanation is given for this question.
ANSWER: D
No explanation is given for this question.
ANSWER: B
Individually calculate the sizes of each member of a structure and make a total to get Full size of
a structure.
};
return 0;
}
ANSWER: B
No explanation is given for this question.
ANSWER: C
No explaination is given for that question.
ANSWER: D
No explaination is given for this question.
78. What are the uses of C Structures?
a) structure is used to implement Linked Lists, Stack and Queue data structures
B) Structures are used in Operating System functionality like Display and Input taking
C) Structure are used to exchange information with peripherals of PC
D) All the above
ANSWER: D
No explaination is given for this question.
ANSWER: D
No explaination is given for this question.
ANSWER: D
No explaination is given for this question.
ANSWER: D
No explaination is given for this question.
===============================================================