C++ Programming Questions and Answers – User Defined Types

«
»
This section on 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 (campus interviews, walkin interviews, company 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.

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

1. Which keyword is used to define the user defined data types?
a) def
b) union
c) typedef
d) type
View Answer

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

2. Identify the correct statement.
a) typedef does not create different types. It only creates synonyms of existing types
b) typedef create different types
c) typedef create own types
d) typedef will not creates synonyms of existing types
View Answer

Answer: a
Explanation: By using typedef, we can create a type of pre-existing type only not our own type of data.

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
View Answer

Answer: d
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.

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         typedef int num;
  6.         num a = 10, b = 15;
  7.         num c = a + b + a - b;
  8.         cout << c;
  9.         return 0;
  10.     }

a) 20
b) 15
c) 30
d) 25
View Answer

Answer: a
Explanation: In this program, we are manipulating the numbers and printing the result using user-defined data types.
Output:

advertisement
$ g++ user.cpp
$ a.out
20

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int i;
  6.         enum month 
  7.         {
  8.             JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,DEC
  9.         };
  10.         for (i = JAN; i <= DEC; i++)
  11.             cout << i;
  12.         return 0;
  13.     }

a) 012345678910
b) 0123456789
c) 01234567891011
d) 01234567891011122
View Answer

Answer: a
Explanation: In this program, we are defined the data types as enumerator and printing its value in a order.
Output:

advertisement
$ g++ user1.cpp
$ a.out
012345678910

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         typedef int num;
  6.         typedef char let;
  7.         let w = "steve";
  8.         num a = 10, b = 15;
  9.         num c = a + w;
  10.         cout << c;
  11.         return 0;
  12.     }

a) 10steve
b) steve10
c) compile time error
d) compile but not run
View Answer

Answer: c
Explanation: Error: invalid conversion from ‘const char*’ to ‘let {aka char}’.

7. What is the syntax of user-defined data types?
a) typedef ExistingDataType NameByUser
b) typedef NameByUser ExistingDataType
c) def NameByUser ExistingDataType
d) def NameByUser ExistingData
View Answer

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

8. How many types of user-defined data type are in c++?
a) 1
b) 2
c) 3
d) 4
View Answer

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

9. What is the scope of typedef defined data types?
a) inside that block only
b) whole program
c) outside the program
d) main function
View Answer

Answer: b
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.

10. How many types of models are available to create the user-defined data type?
a) 1
b) 2
c) 3
d) 4
View Answer

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

Sanfoundry Global Education & Learning Series – C++ Programming Language.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn