100% found this document useful (1 vote)
162 views21 pages

C Important Question

To explore c

Uploaded by

jk716773
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
162 views21 pages

C Important Question

To explore c

Uploaded by

jk716773
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

C-Programming MCQs Related to Switch-Case-Default, Arrays,

Strings, Functions, and Structures


1. What is the ASCII range for A to Z letters?
A. 65 to 90.
B. 48 to 57.
C. 97 to 122
D. 86 to 98.
ANSWER: A

2. The escape sequence \t is a_________.


A. tab.
B. next line.
C. backspace.
D. skip the line.
ANSWER: A

3. Which is the correct statement for finding the cube of 2?


A. pow(2,3);.
B. pow(3,2);.
C. pow(3);.
D. pow(2);.
ANSWER: A

4. The abs() function displays________.


A. absolute value.
B. negative value.
C. zero value.
D. positive value
ANSWER: A

5. The switch statement is used to________.


A. switch between functions in a program.
B. switch from one variable to another variable.
C. choose from multiple possibilities which may arise due to different values of a single variable.
D. use switching variables.
ANSWER: C

6. The default statement is executed in switch case when________.


A. all the case statements are false.
B. one of the case is true.
C. one of the case is false
D. all the case statements are true.
ANSWER: A

7. Each case statement in switch () is separated by______.


A. break.
B. continue.
C. exit().
D. goto.
ANSWER: A

8. The keyword else can be used with________.


A. if statement.
B. switch() statement.
C. do while statement.
D. if..else statement
ANSWER: A

5) C Strings MCQs
9. A string is terminated by ___.

A. Newline ('\n')
B. Null ('\0')
C. Whitespace
D. None of the above

ANSWER: B) Null ('\0')

Explanation:

In C programming language, a string is a sequence of characters terminated with a null


character \0.

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];

printf("Input name: ");


scanf("%s", name);
printf("Given name is: %s", name);

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:

fgets(variable_name, sizeof(size), stdin);

12. Which function is used to concatenate two strings in C?

A. concat()
B. cat()
C. stringcat()
D. strcat()

ANSWER: D) strcat()

Explanation:

In C programming language, the strcat() function is used to concatenate two strings.

Syntax:

strcat(s1, s2);

This function concatenates string s2 onto the end of string s1.

13. What will be the output of the following C code?

#include <stdio.h>

int main()
{
char str1[] = { 'H', 'e', 'l', 'l', 'o' };
char str2[] = "Hello";

printf("%ld,%ld", sizeof(str1), sizeof(str2));

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.

14. What will be the output of the following C code?


#include <stdio.h>

int main()
{
char str1[] = "Hello";
char str2[10];

str2 = str1;

printf("%s,%s", str1, str2);

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

The output will be:

main.c:8:10: error: assignment to expression with array type


8 | 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 function strcmp() is used to compare two strings in C language.

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:

int strcmp(const char* str1, const char* str2);

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:

int strcmpi(const char* str1, const char* str2);

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

ANSWER: A) data_type array_name[array_size];


Explanation:

The correct syntax to declare an array in C is:

data_type array_name[array_size];

19. You can access elements of an array by ___.

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.

20. Which is/are the correct syntax to initialize an array in C?

A. data_type array_name[array_size] = {value1, value2, value3, …};


B. data_type array_name[] = {value1, value2, value3, …};
C. data_type array_name[array_size] = {};
D. Both A and B

ANSWER: D) Both A and B

Explanation:

Both the options A and B are correct to initialize an array.

Example:

int mark[5] = {19, 10, 8, 17, 9};


// or
int mark[] = {19, 10, 8, 17, 9};

21. Array elements are always stored in ___ memory locations.

A. Random
B. Sequential
C. Both A and B
D. None of the above

ANSWER: B) Sequential

Explanation:

Array elements are always stored in sequential memory locations.


22. Let x is an integer array with three elements having value 10, 20, and 30. What
will be the output of the following statement?

printf("%u",x);

A. Prints the value of 0th element (i.e., 10)


B. Prints the garbage value
C. An error occurs
D. Print the address of the array (i.e., the address of first (0th) element

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.

23. What will be the output of the following C program?

#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.

24. What will be the output of the following C program?

#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

ANSWER: C) Garbage value

Explanation:

C language compiler does not check array with its bounds, when an index is out of the
range the garbage value is printed.

25. What will be the output of the following C program?

#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

26. What will be the output of the following C program?

#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.

27. What will be the output of the following C program?

#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

ANSWER: C) Garbage value

Explanation:

C language compiler does not check array with its bounds, when an index is out of the
range the garbage value is printed.

28. If we pass an array as an argument to a function, what actually gets passed?

A. Value of elements in array


B. First element of the array
C. Base address of the array i.e., the address of the first element
D. Address of the last element of array

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

7) C Structures and Union MCQs


29. Which of the following is the collection of different data types?

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.

30. Which operator is used to access the member of a structure?

A. -
B. >
C. *
D. .

ANSWER: D) .

Explanation:

The dot (.) operator is used to access the member of a structure.

31. Which of these is a user-defined data type in C?

A. int
B. union
C. char
D. All of these

ANSWER: B) union

Explanation:

Union is a user defined data type in C.

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.

33. Which keyword is used to define a union?

A. un
B. union
C. Union
D. None of these

ANSWER: B) union

Explanation:

The union is defined using the keyword union.


34. The size of a union is ___.

A. Sum of sizes of all members


B. Predefined by the compiler
C. Equal to size of largest data type
D. None of these

ANSWER: C) Equal to size of largest data type

Explanation:

The size of the union is equal to the size of the largest data type declared in the union.

35. All members of union ___.

A. Stored in consecutive memory location


B. Share same memory location
C. Store at different location
D. All of these

ANSWER: B) Share same memory location

Explanation:

All members of a union in C are stored at the same memory location.

36. Which of the below statements is incorrect in case of union?

A. Union is a user-defined data structure


B. All data share same memory
C. Union stores methods too
D. union keyword is used to initialize

ANSWER: C) Union stores methods too

Explanation:

Unions in C is a data structure which store data types but not the methods.

37. The members of union can be accessed using ___.

A. Dot Operator (.)


B. And Operator (&)
C. Asterisk Operator (*)
D. Right Shift Operator (>)

ANSWER: A) Dot Operator (.)

Explanation:

The dot operator (.) is used to access members of a union.

38. In which case union is better than structure?

A. Less memory is available


B. Faster compilation is required
C. When functions are included
D. None of these

ANSWER: A) Less memory is available

39. Which is the correct syntax to create a union?

A. union union_name {
};
B. union union_name {
}
C. union union_name (
);
D. Union union_name (
)

ANSWER: A)

union union_name {
};

Explanation:

The correct syntax of creating a union is:

union union_name {
};

40. What will be the output of the following C program?

#include <stdio.h>

union values {
int val1;
char val2;
} myVal;

int main()
{
myVal.val1 = 66;

printf("val1 = %p", &myVal.val1);


printf("\nval2 = %p", &myVal.val2);

return 0;
}

A. val1 = 0x54ac88dd2012
val2 = 0x55ac76dd2014
B. Error
C. val1 = 0x55ac76dd2014
val2 = 0x55ac76dd2014
D. Exception

ANSWER: C) val1 = 0x55ac76dd2014


val2 = 0x55ac76dd2014
8) C Functions MCQs
41. What is a function in C?

A. User defined data type


B. Block of code which can be reused
C. Declaration syntax
D. None of these

ANSWER: B) Block of code which can be reused

Explanation:

Function in C is a block of code which can be reused.

42. Functions in C can accept multiple parameters. True or False?

A. True
B. False

ANSWER: A) True

Explanation:

Functions in C programming can accept multiple parameters.

43. Which keyword is used to return values from function?

A. Return
B. Value
C. Return type
D. All of these

ANSWER: C) Return type

Explanation:

The return in declaration is initialized using return type.

44. Which of these is not a valid parameter passing method in C?

A. Call by value
B. Call by reference
C. Call by pointer
D. All of these

ANSWER: C) Call by pointer

Explanation:

Valid parameter passing method in C is:


 Call by value
 Call by reference

45. A C program contains ___.

A. At least one function


B. No function
C. No value from command line
D. All of these

ANSWER: A) At least one function

Explanation:

The main() function is required in the C program.

46. A recursive function in C ___.

A. Call itself again and again


B. Loop over a parameter
C. Return multiple values
D. None of these

ANSWER: A) Call itself again and again

Explanation:

Recursive function in C is a function which calls itself again and again.

47. The scope of a function is limited to?

A. Current block
B. Only function
C. Whole file
D. Directory

ANSWER: C) Whole file

Explanation:

The scope of a function is limited to the file it is declared in.

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)

data_type function_name (parameter){


}

Explanation:

The correct syntax of declaring a function in C:

data_type function_name (parameter){


}

49. The sqrt() function is used to calculate which value?

A. Square
B. Square of reverse bits
C. Square root
D. None of these

ANSWER: C) Square root

Explanation:

The sqrt() method in C is used to calculate the square root of a number.

50. What will be the output of the following C program?

#include <stdio.h>

int myFunc(int x){


return (--x);
}

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

52. The structure combines variables of_______.


A. similar data types.
B. dissimilar data types.
C. unsigned data types.
D. signed data types.
ANSWER: B

===============================================

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.

54. Choose a correct statement about C Function?


void main() {
printf("Hello");
}

a) "main" is the name of default must and should Function


b) main() is same as int main()
c) By default, return 0 is added as the last statement of a function without specific return type
d) All the above

ANSWER: D
No explanation is given for this question.

55. A function which calls itself is called a ___ function.


a) Self Function
b) Auto Function
c) Recursive Function
d) Static Function

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.

57. How many times the program will print "Algbly"?

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.

58. Determine Output :


void show() {
printf("PISTA ";
show();
}

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.

59. What are the types of functions in C Language?


a) Library Functions
b) User Defined Functions
c) Both Library and User Defined
d) None of the above

ANSWER: C
No explanation is given for this question.

60. Choose correct statements about C Language Pass By Value.


a) Pass By Value copies the variable value in one more memory location
b) Pass By Value does not use Pointers
c) Pass By Value protects your source or original variables from changes in outside functions or
called functions
d) All the above

ANSWER: D
No explanation is given for this question.

61. What is the limit for number of functions in a C Program?


a) 16
b) 31
c) 32
d) No Limit

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.

64. Which among the following is Copying function?


a) memcpy()
b) strcopy()
c) memcopy()
d) strxcpy()

ANSWER: A
The memcpy() function is used to copy n characters from the object.

65. The _______ function appends not more than n characters.


a) strcat()
b) strcon()
c) strncat()
d) memcat()

ANSWER: C
The strncat() function appends not more than n characters from the array.

66. What will strcmp() function do?


a) compares the first n characters of the object
b) undefined function
c) copies the string
d) compares the string

ANSWER: D
The strcmp() function compares the string s1 to the string s2.

67. What is a String in C Language?


a) String is a new Data Type in C
b) String is an array of Characters with null character as the last element of array
c) String is an array of Characters with null character as the first element of array
d) String is an array of Integers with 0 as the last element of array

ANSWER: B
No explaination is given for that question.

68. Choose a correct statement about C String.


char ary[] = "Hello......!";

a) Character array, ary is a string


b) ary has no Null character at the end
c) String size is not mentioned
d) String can not contain special characters

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.

char ary[] = {'h','e','l','l','o','\0'};


68. What is the Format specifier used to print a String or Character array in C Printf or Scanf function?
a) %c
b) %C
c) %s
d) %w

ANSWER: C
No explaination is given for this question.

69. What will be the output of the following piece of code?


int main() {
char ary[] = "Discovery Channel";
printf("%s", ary);
return 0;
}

a) D
b) Discovery Channel
c) Discovery
d) Compiler error

ANSWER: B
%s prints the while character array in one go.

70. What is the output of C Program with Strings?


int main() {
char str[] = {'g', 'l', 'o', 'b', 'a', 'l'};
printf("%s", str);
return 0;
}

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

72. What will be the size of the following structure?


#include <stdio.h>
struct temp {
int a[10];
char p;
};

a) 5
b) 11
c) 41
d) 44

ANSWER: D
No explanation is given for this question.

73. What is a structure in C language?


a) A structure is a collection of elements that can be of same datatype
b) A structure is a collection of elements that can be of different datatype
c) Elements of a structure are called members
d) All of the these

ANSWER: D
No explanation is given for this question.

74. What is the size of a C structure?


a) C structure is always 128 bytes
b) Size of C structure is the totatl bytes of all elements of structure
c) Size of C structure is the size of largest elements
d) None of the above

ANSWER: B
Individually calculate the sizes of each member of a structure and make a total to get Full size of
a structure.

75. Choose a correct statement about C structure?


int main() {
struct ship {

};
return 0;
}

a) It is wrong to define an empty structure


b) Member variables can be added to a structure even after its first definition
c) There is no use of defining an empty structure
d) None of the above

ANSWER: B
No explanation is given for this question.

76. Choose a correct statement about C structure elements?


a) Structure elements are stored on random free memory locations
b) structure elements are stored in register memory locations
c) structure elements are stored in contiguous memory locations
d) None of the above

ANSWER: C
No explaination is given for that question.

77. A C structure or User defined datatype is also called ________.


a) Derived data type
b) Secondary data type
c) Aggregate data type
d) All the above

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.

79. Choose a correct statement about C structures.


a) A structure can contain same structure type member
b) A structure size is limited by only physical memory of that PC
c) You can define an unlimited number of members inside a structure
d) All the above

ANSWER: D
No explaination is given for this question.

80. Which of the following return-type cannot be used for a function in C?


a) char *
b) struct
c) void
d) none of the mentioned

ANSWER: D
No explaination is given for this question.

81. Which of the following is not possible under any scenario?


a) s1 = &s2;
b) s1 = s2;
c) (*s1).number = 10;
d) None of the mentioned

ANSWER: D
No explaination is given for this question.

===============================================================

You might also like