Module I MCQ CPII
Module I MCQ CPII
Prof. Manjunath
1
1. Which of the following is the correct result when accessing different members of the
union?
union Data {
int i;
float f;
char str[20];
};
union Data data;
data.i = 10;
data.f = 3.14;
A) The value of data.i will be 10, data.f will be 3.14, and data.str will be empty.
B) Only data.f will hold a valid value, data.i and data.str will be overwritten.
C) All members will hold the same value.
D) Compilation error.
Answer: B) Only data.f will hold a valid value, data.i and data.str will be overwritten.
3 3. What is the effect of assigning a new value to one member of a union after assigning
to another member?
A) The new value overwrites the previous value in the same memory location.
B) The new value is stored in a separate memory location.
C) The previous value is preserved.
D) Undefined behavior.
Answer: A) The new value overwrites the previous value in the same memory location.
4 4. Which of the following can be used to assign a value to an enum constant explicitly?
A) Only the first constant in the enum can be assigned a value.
B) Enum constants can only have sequential integer values starting from 0.
C) Enum constants can be assigned specific integer values explicitly.
D) All enum constants are initialized with character values.
Answer: C) Enum constants can be assigned specific integer values explicitly.
5 5. Given the following enum definition, What will be the value of Monday in this enum?
enum Week { Sunday = 1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
A) 0
B) 1
C) 2
D) 3
Answer: C) 2
Prof. Pradeep
1
Which of the following is the correct way to declare a string in C?
A. char name;
B. string name = "John";
C. char name[] = "John";
D. char name[4] = 'John';
✅ Correct Answer: C
2
What is the output of the following code?
str[1] = '\0';
printf("%s", str);
A. abc
B. a
C. b
D. bc
✅ Correct Answer: B
3
What is the output of the following code?
printf("%s", str);
A. Hello
B. Hell
C. Undefined behavior
D. Compilation error
✅ Correct Answer: C
4
What is the output?
printf("%d", sizeof(s));
A. 3
B. 4
C. 10
D. Depends on compiler
✅ Correct Answer: C
5
Output of the following?
Char str[3]=”Hello”;
✅ Correct Answer: A
6
Output of the following?
#include <stdio.h>
#include <string.h>
int main() {
printf("%d\n", strlen(str));
return 0;
A. 1
B. 0
C. NULL
D. \0
✅ Correct Answer: B
7
What will be the size of the structure student in memory (assuming an int takes 4 bytes and a
char takes 1 byte)?
struct student {
int id;
char name[20];
};
A. 20 Bytes
B. 24 Bytes
C. 5 Bytes
D. 21 Bytes
✅ Correct Answer: B
8 Structures in C can contain:
✅ Correct Answer: A
9 What does the following code do?
✅
D. Creates a new Structure named Student
Correct Answer: C
10
What is the size of the following union?
union test {
int a;
char b;
float c;
};
A) 9 bytes
B) 1 byte
C) 4 bytes
D) 12 bytes
✅ Correct Answer: C
11 Which of the following initializes a structure variable stu with id as 100 and name as “John”?
struct student {
int id;
char name[20];
};
✅ Correct Answer: A
12
What will happen if you assign a value to one member of a union?
✅ Correct Answer: C
13
Which statement is true about accessing nested unions inside structures?
struct A {
int x;
union {
int y;
float z;
} u;
};
A) A.z
B) A->u.z
C) A.u.z
D) A->z
✅ Correct Answer: C
14
Which of the following is true about structures in C?
✅ Correct Answer: C
Prof. Gourish
1 #include <stdio.h>
#include <string.h>
int main() {
char str[20] = "Hello";
strcat(str, "World");
printf("%s", str);
return 0;
}
• A. Hello
• B. Hello World
• C. HelloWorld
• D. Compilation Error
✅ Answer: C. HelloWorld
Explanation: strcat() concatenates "World" to "Hello". No space is added automatically.
#include <stdio.h>
#include <string.h>
int main() {
char str[10] = "ABC";
printf("%d", strlen(str));
return 0;
}
```
• A. 10
• B. 4
• C. 3
• D. 0
✅ Answer: C. 3
Explanation: strlen(str) returns the number of characters before the null character, not
the array size.
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "abc";
char str2[] = "ABC";
printf("%d", strcmp(str1, str2));
return 0;
}
• A. 0
• B. >0 (positive value)
• C. <0 (negative value)
• D. Compilation Error
int main() {
char str[5];
strcpy(str, "hello");
printf("%s", str);
return 0;
}
• A. hello
• B. Compilation Error
• C. Undefined behavior
• D. h
✅ Answer: C. Undefined behavior
Explanation: "hello" needs 6 bytes (including null), but only 5 are allocated → overflow.
#include <stdio.h>
struct S {
int x;
char str[10];
};
int main() {
struct S s1 = { 5, "Hi" };
printf("%d %s", s1.x, s1.str);
return 0;
}
• A. 5Hi
• B. 5 Hi
• C. Hi 5
• D. Compilation Error
✅ Answer: B. 5 Hi
Explanation: %d prints x, and %s prints the string "Hi". A space is printed by the format
string.
#include <stdio.h>
int main() {
enum week day = Wed;
printf("%d", day);
return 0;
}
• A. 2
• B. 3
• C. Wed
• D. Compilation Error
✅ Answer: A. 2
Explanation: Enumerators start from 0 by default, so `Wed` has value 2.
#include <stdio.h>
union data {
int i;
float f;
};
int main() {
union data d;
d.i = 10;
d.f = 3.5;
printf("%d", d.i);
return 0;
}
• A. 10
• B. 3.5
• C. Garbage value
• D. Compilation Error
11 #include <stdio.h>
typedef struct {
int x;
float y;
} Point;
int main() {
Point p = {10, 20.5};
printf("%d %.1f", p.x, p.y);
return 0;
}
• A. 10 20.5
• B. 10.0 20.5
• C. 20.5 10
• D. Compilation Error
✅ Answer: A. 10 20.5
Explanation: `typedef` simplifies the structure declaration.
#include <stdio.h>
int main() {
State s = ON;
printf("%d", s);
return 0;
}
• A. 0
• B. 1
• C. ON
• D. Compilation Error
✅ Answer: B. 1
Explanation: `ON` is the second item in the enum, so its value is 1.
Prof. Pratik
1
Which of the following statements is true about structures.
A. A structure can be compared with another structure
B. Arrays cannot be declared within structures
C. Structures is a collection of variables of different datatypes
D. Variables are accessed using indirection (*) operator
2
Choose the correct statement with respect to the following structure declaration
struct book
{
char title[50];
char author[20];
char publisher[20];
int pubyr;
};
struct book book1, book2;
Solution: B. The dot operator can be used to initialize individual members. This
type of initialization is called designated initialization. book1 and book2 are
structure variables of type ‘struct book’. ‘book’ is the structure name or tag. It has
four members - three character arrays and one integer. title is a character array
and the assignment operator cannot be used to initialize it. Instead use
strcpy(title, “C Prog”);
3
Which of the following is true regarding structures and functions
A. Structure members that are not arrays are passed by value
B. C does not allow passing entire structures
C. Structures are not allowed as return type
D. -> operator is used to pass members as arguments
Solution: A. Structure members that are not arrays are passed by value. Arrays
are always passed by reference. Return type of structure is made possible using
keyword ‘struct’ before function name. Entire structures can be passed as
arguments or individual members using dot operator.
4
Which of the following is the correct way to call function ‘library’ and what is its
return type?
struct borrowinfo library(struct book); //function prototype
struct book book1;
A. library(book) and return type is ‘struct book’
B. library(book1) and return type is ‘struct borrowinfo’
C. library(struct book) and return type is ‘struct borrowinfo’
D. library(book1.title) and return type is ‘struct book’
Solution: B.The function library expects parameter of type ‘struct book’. ‘book1’ is
a structure variable of type ‘struct book’. Return type is specified before function
name.
5
Which of the following statement is true about unions
A. A union is a structure which is allocated single area of storage
B. Size of a union is the greater than or equal of the widest member
C. Only last stored type can be retrieved
D. All of the above
Solution: D. Since union has a single area of storage, a union with members of
type ‘int’, ‘float’ and ‘char ’ will have size of atleast 4 bytes and at any point of
time one of them can be stored. Only the last stored type can be retrieved.
6
Which of the statement is true about ‘typedef’
A. Allows creation of new datatype names
B. Datatypes larger than 4 bytes cannot be created
C. Interpreted by the preprocessor
D. Cannot be used to substitute pointers
A. 1 11
B. 2 12
C. 1 12
D. 0 0
8
What will be the output of the following?
printf(“#%3.9s#, #%7.3s#”, “MORNING”, “NIGHT”);
A. #MORNING#, # NIG#
B. #MOR#, #NIGHT#
C. # MORN#, #NIGHT#
D. #MOR #,# NI#
#%7.3s# stands for minimum field width of 7 and number of characters to print is
3. Therefore # NIG#
9
Which of the following statements is true about library functions?
Solution: C. scanf() skips white space in beginning and stops reading upon
encountering blank space. strlen() returns length excluding null character.
strcat() can be used for concatenating or appending strings. It returns a new
string. toupper() converts a lower case character to upper case. It expects a
character argument.
10
What will be the output of the following?
A. 1 -1
B. -1 1
C. -1 -1
D. 1 1
Solution: A. Since ASCII of ‘G’> ‘F’, therefore “DEFG”> “DEFFI” even though first
string is shorter than second. ASCII of ‘A’< ‘a’, therefore “ABC”< “abc”.
Prof. Sowmya.B
#include <stdio.h>
struct test {
int x = 0;
char y = 'A';
};
int main()
{
struct test t;
printf("%d, %c", s.x, s.y);
return 0;
}
A. 0
B. Error
C. garbage value garbage value
D. None of these
Answer: B
8 Which of the following functions is more appropriate for reading in a multi-word string?
A. scanf()
B. printf()
C. gets()
D. puts()
Answer: C
#include <stdio.h>
#include<string.h>
void main()
{
char s1[20] = "Hello", s2[10] = " World";
printf("%s", strcpy(s2, strcat(s1, s2)));
}
A. Hello World
B. HelloWorld
C. Hello
D. World
Answer: A
Prof. Padmaja
1 1. What is a string in C?
a) A collection of characters
b) A single character
c) An integer data type
d) A special function
Answer: A
a) 44
b) 42
c) 24
d) Compilation error
Answer:
7 Structures
1. What will be the output (assume integers are 4 bytes, no padding between members,
and structure starts at address 1004)?
a) 1004 1008 1058
b) 1004 1009 1060
c) 1004 1054 1104
d) 1004 1044 1084
Answer : A
3. What will be the size of the following structure?
a) 5
b) 11
c) 41
d) 44
Answer: D
9 1. A user defined data type, which is used to assign names to integral constants is
called ____________
a)Union
b)Array
c)Structure
d) Enum
Answer: D
a) 5
b) 6
c) 7
d) 8
Answer: B
Prof. Chandrika
1 Which keyword is used to declare the structure in C?
a) structure
b)structures
c)struct_name
d)struct
Answer:d)
2 Which keyword is used to create an alias name for data types in structures?
a) typedef
b) struct
c) structures
d) duplicate
Answer: a
3 Which operator is used to copy the contents of one structure variable to another
structure variable ?
a)+
b)=
c)->
d) .
Answer:b
Struct student
{
Char name[20];
int rollno;
};
a)50
b)20
c)22
d)2
Answer:c)22
Explanation: int 2 byte +char 1 byte*20(20)=22 bytes
6 The correct syntax to access the member of the ith structure in the array of structures
is?
Struct temp
{
int b;
}s[50];
a) S.b.[i];
b) S.[i].b;
c) S.b[i];
d) s[i].b;
Answer: d) s[i].b
10 What would be the size of the following union declaration?(assume the size of
double=8,size of int=2,size of char=1)
#include<stdio.h>
Union u temp
{
double a;
int b[10];
char c;
}u;
a)4
b)8
c)20
Answer: c)20
Explanation: The size of a union is determined by a size of the biggest member in the
union
Bigger size=int b [10];
=2*10=20
11 What will be the output of the following code?(assuming the size of int is 2 and float is 4)
Union h
{
int i val;
Float f val;
}u;
void main()
{
printf(“%d”,size of(u));
}
a) 16
b) 8
c) 4
d) 32
Answer :c) 4
Explanation: union will take the largest data type which is float.so 4 will be printed.
14 By default,what is the starting value assigned to the first name in an enum if not
specified?
a) -1
b) 0
c) 1
d) Garbage value
Answer:b) 0