C++ Programming Questions and Answers – User Defined Types

C++ MCQs (multiple choice questions) focuses on “User Defined Types”. One shall practice these MCQs to improve their C++ programming skills needed for various interviews , placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our multiple choice questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

C multiple choice questions on “User Defined Types” along with answers, explanations and/or solutions:

Q 1. Which keyword is used to define the user defined data types?
A. def
B. union
C. type
D. Typedef

Show Answer Answer:-D. Typedef
Explanation Typedef is used to define user defined datatypes. eg: typedef int INT; INT a; here INT is used defined data type.

Q 2. Identify the correct statement.
A. typedef will not creates synonyms of existing types
B. typedef create different types
C. typedef create own types
D. typedef does not create different types. It only creates synonyms of existing types

Show Answer Answer:-D. typedef does not create different types. It only creates synonyms of existing types
Explanation By using typedef, we can create a type of pre-existing type only not our own type of data.

Q 3. What does the data type defined by union will do?
A. It allow one different portion of memory to be accessed as same data types
B. It allow one same portion of memory to be accessed as same data types
C. It allow one different portion of memory to be accessed as different data types
D. It allow one same portion of memory to be accessed as different data types

Show Answer Answer:- D.It allow one same portion of memory to be accessed as different data types
Explanation Union is used to define the data types of our choice and it will store the data type in one location make them accessible.

Q 4. What will be the output of the following C++ code?

#include <iostream>

using namespace std;

int main()

{

typedef int num;

num a = 10, b = 15;

num c = a + b + a – b;

cout << c;

return 0;

}

A. 10
B. 20
C. 30
D. 25

Show Answer Answer:-B. 20
Explanation In this program, we are manipulating the numbers and printing the result using user-defined data types. Output: $ g++ user.cpp $ a.out 20

Q 5. What will be the output of the following C++ code?

#include <iostream>

using namespace std;

int main()

{

int i;

enum month

{

JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,DEC

};

for (i = JAN; i <= DEC; i++)

cout << i;

return 0;

}

A. 012345678910
B. 0123456789
C. 01234567891011
D. 01234567891011122

Show Answer Answer:-A. 012345678910
Explanation In this program, we are defined the data types as enumerator and printing its value in a order. Output: $ g++ user1.cpp $ a.out 012345678910

Q 6. What will be the output of the following C++ code?

#include <iostream>

using namespace std;

int main()

{

typedef int num;

typedef char let;

let w = “steve”;

num a = 10, b = 15;

num c = a + w;

cout << c;

return 0;

}

A. 10steve
B. steve10
C. compile time error
D. compile but not run

Show Answer Answer:-C. compile time error
Explanation Error: invalid conversion from ‘const char*’ to ‘let {aka char}’.

Q 7. What is the syntax of user-defined data types?
A. def NameByUser ExistingData
B. typedef NameByUser ExistingDataType
C. def NameByUser ExistingDataType
D. typedef ExistingDataType NameByUser

Show Answer Answer:-D. typedef ExistingDataType NameByUser
Explanation correct syntax is typedef ExistingDataType NameByUser. typedef int INT; (typedef existing-datatype New-name;).

Q 8. How many types of user-defined data type are in c++?
A. 1
B. 2
C. 3
D. 4

Show Answer Answer:-C. 3
Explanation There are three types of user-defined data types. They are typedef, union, enumerator.

Q 9. What is the scope of typedef defined data types?
A. inside that block only
B. outside the program
C. whole program
D. main function

Show Answer Answer:-C. whole program
Explanation We are defining the user-defined data type to be availed only inside that program, if we want to use anywhere means we have to define those types in the header file.

Q 10. How many types of models are available to create the user-defined data type?
A. 1
B. 2
C. 3
D. 4

Show Answer Answer:-B. 2
Explanation There are two types of models. They are references to built-in types and multipart types.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page