0% found this document useful (0 votes)
46 views34 pages

Module I MCQ CPII

The document contains multiple-choice questions (MCQs) related to C programming, specifically focusing on strings, structures, unions, enums, and typedefs. Each question is followed by the correct answer, providing insights into the expected knowledge for C programming II. The content is structured as a practice test for students to assess their understanding of the topics.

Uploaded by

ms0602487
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
0% found this document useful (0 votes)
46 views34 pages

Module I MCQ CPII

The document contains multiple-choice questions (MCQs) related to C programming, specifically focusing on strings, structures, unions, enums, and typedefs. Each question is followed by the correct answer, providing insights into the expected knowledge for C programming II. The content is structured as a practice test for students to assess their understanding of the topics.

Uploaded by

ms0602487
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/ 34

C Programming II with Linux - Practice MCQs

Module 1 ( Strings, Structures, Unions, enum and typedef)

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

2 2. Which of the following is the maximum size of a union in C?​ ​ ​


​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
A) The size of the smallest member.​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​
B) The sum of the sizes of all members.​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​
C) The size of the largest member.​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​
D) Undefined.​ ​ ​ ​ ​ ​ ​ ​ ​ ​

​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
Answer: C) The size of the largest member.​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​

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​ ​ ​ ​ ​ ​ ​ ​ ​

6 6. What is the output of the following C code?​ ​ ​ ​ ​


​ ​ ​ ​ ​
char str1[] = "Hello";​ ​ ​ ​ ​
char str2[] = "World";​ ​ ​ ​ ​
strcat(str1, str2);​ ​ ​ ​ ​
printf("%s", str1);​ ​ ​ ​ ​
​ ​ ​ ​ ​
A) "HelloWorld"​ ​ ​ ​ ​
B) "WorldHello"​ ​ ​ ​ ​
C) "Hello"​ ​ ​ ​ ​
D) Compilation error​ ​ ​ ​ ​
​ ​ ​ ​ ​
Answer: A) "HelloWorld"​ ​ ​ ​ ​

7 7. What is the correct way to compare two strings in C?​ ​ ​ ​


​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​
A) str1 == str2​​ ​ ​ ​ ​ ​ ​
B) strcmp(str1, str2) == 0​ ​ ​ ​ ​ ​ ​ ​
C) str1.equals(str2)​ ​ ​ ​ ​ ​ ​ ​
D) str1.compare(str2)​​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​
Answer: B) strcmp(str1, str2) == 0​ ​ ​ ​ ​ ​ ​

8 8. What is the primary purpose of a structure in C?​ ​ ​ ​ ​


​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​
A) To store data of different types together​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​
B) To store data of the same type together​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​
C) To store a fixed set of functions​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​
D) To create arrays of data​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​
Answer: A) To store data of different types together​​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​ ​

9 9. What is the correct way to declare a structure in C?​ ​ ​ ​


​ ​ ​
​ ​ ​ ​ ​ ​ ​
A) structure Person { char name[20]; int age; };​ ​ ​ ​ ​
​ ​
B) struct Person { char name[20]; int age; };​ ​ ​ ​ ​ ​

C) Person { char name[20]; int age; };​ ​ ​ ​ ​ ​

D) struct { name[20]; age; };​ ​ ​ ​ ​ ​ ​
Answer: B) struct Person { char name[20]; int age; };​ ​ ​ ​
​ ​ ​
​ ​ ​ ​ ​ ​ ​

10 10. What is the output of the following code snippet?​ ​ ​ ​


​ ​
​ ​ ​ ​ ​ ​
struct Test {​ ​ ​ ​ ​ ​
int a;​ ​ ​ ​ ​ ​
char b;​ ​ ​ ​ ​ ​
} t = {10, 'A'};​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​
printf("%d %c", t.a, t.b);​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​
A) 10 A​ ​ ​ ​ ​ ​
B) A 10​ ​ ​ ​ ​ ​
C) Compilation Error​ ​ ​ ​ ​ ​
D) Undefined​ ​
​ ​ ​ ​
Answer: A) 10 A​ ​ ​ ​ ​ ​

11 11. What is the output of the following code?​​ ​ ​ ​


​ ​ ​ ​ ​
typedef int INTEGER;​​ ​ ​ ​
INTEGER x = 5;​ ​ ​ ​ ​
printf("%d", x);​​ ​ ​ ​
​ ​ ​ ​ ​
A) Error: INTEGER is not defined​ ​ ​ ​ ​
B) 5​ ​ ​ ​ ​
C) Garbage value​ ​ ​ ​ ​
D) Compilation error​ ​ ​ ​ ​
​ ​ ​ ​ ​
Answer: B) 5​ ​ ​ ​ ​

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?

char str[20] = "abc";

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?

char str[5] = "Hello";

printf("%s", str);

A. Hello​
B. Hell​
C. Undefined behavior​
D. Compilation error

✅ Correct Answer: C
4
What is the output?

char s[10] = "abc";

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

A. Compile Time error​


B. Hello​
C. Hello\0​
D. Hel

✅ Correct Answer: A
6
Output of the following?

#include <stdio.h>

#include <string.h>

int main() {

char str[] = "\0";

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:

A. Only Data members

B. Data members and Member function

C. Only member functions

D. None of the above

✅ Correct Answer: A
9 What does the following code do?

typedef struct student {


​ int id;
​ char name[20];
} Student;

A. Defines a function named student​


B. Defines a Variable named student​
C. Creates an alias ‘Student’ for the structure​


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

};

A) struct student stu = {100, “John”};

B) struct student stu = {id: 100, name: “John”};

C) student stu = {100, “John”};

D) student stu = {id = 100, name = “John”};

✅ Correct Answer: A

12
What will happen if you assign a value to one member of a union?

A) All members get updated​


B) Other members become inaccessible​
C) The last assigned member holds the valid value​
D) All values are stored separately

✅ Correct Answer: C
13
Which statement is true about accessing nested unions inside structures?

struct A {

​ int x;

​ union {

​ int y;

​ float z;

​ } u;

};

To access z, you should write:

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?

A) Members share the same memory space​


B) Members cannot be different data types​
C) Members are stored in contiguous memory​
D) You can't have pointers to structures

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

2 Which of the following correctly defines a structure in C?


• A. struct { int a; float b; }
• B. structure sample { int a; }
• C. struct sample ( int a; );
• D. struct sample { int a; };

✅ Answer: D. struct sample { int a; };


Explanation: Proper syntax for defining a structure uses the `struct` keyword followed by
the name and body in `{}`.

3 Consider the code below. What will be printed?

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

4 What will the following code output?

#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

✅ Answer: B. >0 (positive value)


Explanation: 'a' > 'A' in ASCII, so strcmp("abc", "ABC") returns a positive value.

5 What will be the result of the following code?


#include <stdio.h>
#include <string.h>

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.

6 What is the output of the code below?

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

7 What is the output of the following code?

#include <stdio.h>

enum week {Mon, Tue, Wed, Thu, Fri};

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.

8 What is the purpose of `typedef` in C?


• A. It defines a new structure
• B. It declares global variables
• C. It defines new data type names
• D. It allocates memory dynamically

✅ Answer: C. It defines new data type names


Explanation: `typedef` is used to assign new names to existing types to increase code
clarity.

9 What will be the output of this union-based code?

#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

✅ Answer: C. Garbage value


Explanation: Only one member of a union can hold a value at a time. Assigning `d.f`
overwrites `d.i`.

10 Which of the following is a valid `typedef` declaration?


• A. typedef int age;
• B. typedef int = age;
• C. typedef age int;
• D. type int age;
✅ Answer: A. typedef int age;
Explanation: Correct syntax is `typedef existing_type new_name;`.

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.

12 Which of the following is TRUE about unions?


• A. All members occupy separate memory
• B. They store values of all members simultaneously
• C. Size of union is size of its largest member
• D. `union` can only be used inside structures

✅ Answer: C. Size of union is size of its largest member


Explanation: Union allocates memory equal to its largest member since members share
memory.

13 What will be the output of the following?

#include <stdio.h>

typedef enum {OFF, ON} State;

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

Solution: C. Variables in a structure are called members. They can be of different


datatypes including arrays and pointers. Only structure members of similar
datatype can be used for comparison. Members are accessed using dot (.)
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;

A. book1 and book2 are variables of type ‘char’


B. struct book book1 = {.title=“C Prog”} can be used for initialization
C. Structure ‘book’ has 20 members
D. book1.title = “C Prog” can be used for initialization

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

Solution: A. ‘typedef’ is used to create alternate names for known datatypes. It


does not create new datatypes. Since the declaration doesn’t start with #, it is
interpreted by the compiler. Useful in creating substitutes for machine-dependent
datatypes, pointers, pointers to functions etc.
7
What will be output of the following?
enum books {KRC, KING, KANETKAR=3, HAYKIN=11,PROAKIS};
printf(“%u %u”,KING, PROAKIS);

A. 1 11
B. 2 12
C. 1 12
D. 0 0

Solution: C. Auto generated counting starts at 0, and continues progressively.


Hence KING =1. Since counting continues after the last specified value
PROAKIS=12.

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#

Solution: A. #%3.9s# stands for minimum field width of 3 and number of


characters to print is 9. Therefore #MORNING#

#%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?

A. scanf() doesn’t skip white space, stops reading upon encountering‘\n’


B. strlen() returns length including null character
C. strcat() can be used for appending strings
D. toupper() expects a string argument

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?

int i =strcmp(“DEFG”, “DEFFI”);


int j =strcmp(“ABC”, “abc”);
printf(“%d %d”, i, j);

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

1 Which operator connects the structure name to its member name?


A. -​
B. ->​
C. .​
D. both . and ->
Answer: C

2 Which of the following are themselves a collection of different data types?


A. String​
B. Structures​
C. Char​
D. None of the above
Answer: B

3 Which of the following is a properly defined struct?


A. struct {int a;}​
B. struct a_struct {int a;}​
C. both​
D. None
Answer: D

4 What is the output of this program?

#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

5 What is the output of this program?


#include <stdio.h>
struct result{
char sub[20];
int marks;
};
void main()
{
struct result res[] = { {"Maths",100},
{"Science",90},
{"English",85}
};
printf("%s ", res[1].sub);
printf("%d", (*(res+2)).marks);
}
A. Maths 100
B. Science 85
C. Science 90
D. Science 100
Answer:B

6 Union differs from structure in the following way


A. All members are used at a time​
B. Only one member can be used at a time​
C. Union cannot have more members​
D. Union initialized all members as structure
Answer:B

7 Members of a union are accessed as________________.


A. union-name.member​
B. union-pointer->member​
C. Both a & b​
D. None of the mentioned
Answer:C

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

9 If the two strings are identical, then strcmp() function returns


A. -1​
B. 1​
C. 0​
D. None
Answer:C

10 What is the output of this program?

#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

2 3. What is the null character in C?


a) '0'
b) '\0'
c) 'NULL'
d) 0
Answer: B

3 8. What is the return type of the strlen() function in C?


a) int
b) size_t
c) long
d) char*
Answer: A
4 13. What happens if a string array is initialized with fewer characters than its size in C?
a) The remaining elements are filled with null characters
b) The remaining elements are left uninitialized
c) It causes an error
d) The program crashes
Answer: A

5 16. Which of the following correctly initializes an empty string in C?


a) char str[] = "";
b) char str[] = " ";
c) char str[] = {'\0'};
d) Both a) and c)
Answer: D

6 23. What will be the output of the following?

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

8 9. 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

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

10 8.What will be the value of BLUE in the following enum?

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

4 Which operator is used to access the members of the structure?


a)Dot
b)Assignment
c)sizeof
d)logical
Answer: a

5 How much memory will the given structure hold?

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

7 Choose the correct statement from the following?


Struct student
{
int rollno;
Char name[20];
}std;
a)​ printf(“Name=%s”,name);
b)​ printf(“Name=%c”,name);
c)​ printf(“Name=%s”,std.name);
d)​ printf(“Name=%s”,name.std);
Answer: c
Explanation: the structure members can be accessed with the variable name and a dot
operator

8 Which of the following can not be a structure member?


a)​ Function
b)​ Array
c)​ Structure
d)​ union
Answer:a)Function
Explanation:cannot write functions inside the structure

9 What is the output of a C program with structures?


Int main()
{
Struct stock
{
int items;
char name[10];
}a;
strcpy(a.name,”Pen”);
a.items=20;
printf(“Name=%s items=%d”,a.name,a.items);
return 0;
}
a)​ Name=Pen items=20
b)​ Garbage value
c)​ Compilation error
Answer:a

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.

12 How many bytes will consume memory of a given union?


union student
{
int rollno;
char name[20];
float marks;
};
a)​ 22
b)​ 26
c)​ 20
d)​ None of the above
Answer:c)20

13 Identify the correct statement?


a)​ union can be member of structure
b)​ Structure can be member of union
c)​ Both a and b
d)​ None of the above
Answer : c

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

15 Which keyword is used to create an enumeration in C?


a)​ struct
b)​ enum
c)​ union
d)​ typedef
Answer: b)enum

16 What is the correct output of the following code?


#include<stdio.h>
enum numbers{ONE=5,TWO,THREE);
Int main()
{
printf(“ %d %d %d”,ONE,TWO,THREE);
return 0;
}
a)​ 5 6 7
b)​ 1 2 3
c)​ 5 5 5
d)​ 5 7 9
Answer: a) 5 6 7
17 Which of the following is true about enum constants?
a)​ They are stored as integers
b)​ They are stored as characters
c)​ They are stored as float
d)​ None of the above
Answer:a)They are stored as integers

18 Consider the below code snippet,what is the value of Thu?


enum week{Mon,Tue,Wed,Thu,Fri,Sat,Sun};
a)​ 3
b)​ 2
c)​ 4
d)​ 5
Answer: 3

19 Which of the following character ends the string?


a)​ ‘0’
b)​ ‘\0’
c)​ ‘\n’
d)​ None of the above
Answer: b) Null character

20 Which function is used to convert a string to an integer in C?


a)​ atoi()
b)​ itoa()
c)​ str2int()
d)​ int2str()
Answer:a) atoi()

21 When considering the null character,what size is the string “Hello” ?


a)​ 6
b)​ 4
c)​ 7
d)​ 5
Answer: d)5

22 How do two strings get compared?


a)​ compare(str1,str2)
b)​ strcomp(str1,str2)
c)​ strcmp(str1,str2)
d)​ str1=str2
Answer: c

23 Which function copies a string from one variable to another?


a)​ copy()
b)​ Strncpy()
c)​ strcpy()
d)​ strcpy()
Answer: d)strcpy()

24 Which function is used to determine the length of a string?


a)​ length()
b)​ strlength()
c)​ len()
d)​ strlen()
Answer: d) strlen()

25 What will be the output of the following code segment?


Char str[20]=”programming”;
printf(“%s,str[3]”);
a)​ programming
b)​ prog
c)​ pro
d)​ g
Answer: d)g

26 Which library or header file is used for string functions?


a)​ stdio.h
b)​ ctype.h
c)​ string.h
d)​ conio.h
Answer: c) string.h

27 What will be the size of the string? Char s[]=”i am programmer”;


a)​ 3
b)​ 15
c)​ 16
d)​ 20
Answer: c)16

28 What is the correct way to declare a string in C?


a)​ char str[10];
b)​ string str;
c)​ char str;
d)​ str[10];
Answer: a) char str[10];

29 Which function joins(concatenates) two strings?


a)​ strcpy()
b)​ strcmp()
c)​ strcat()
d)​ strjoin()
Answer: c)strcat()

30 What is the size of the array char str[6]=”World”;?


a)​ 5
b)​ 6
c)​ 7
d)​ 4
Answer:b)6 //(5 characters+1 null terminator))

31 What would be the output of this code?


#include<stdio.h>
#include<string.h>
int main()
{
char str1[10]=”Hi”;
char str2[10];
strcpy(str2,str1);
printf(“%s”,str2);
return 0;
}
a)​ Hi
b)​ str1
c)​ str2
d)​ Compilation error
Answer: a) Hi

32 Output of this code will be?


#include<stdio.h>
#include<string.h>
int main()
{
char str1[20]=”Good”;
char str2[]=”Morning”;
strcat(str1,str2);
printf(“%s”,str1);
return 0;
}
a)​ Good
b)​ Morning
c)​ GoodMorning
d)​ Good Morning
Answer: c) GoodMorning
33 Which is correct about this code?
#include<stdio.h>
int main() {
char s[]={‘a’,’b’,’c’,’\0’};
printf(“%s”,s);
return 0;
}
a)​ Prints abc
b)​ Compilation error
c)​ Prints garbage
d)​ Runtime error
Answer: a) prints abc //proper manual null termination.

You might also like