C++ Programming MCQ – Quizs Question Structures

Q 1. The data elements in the structure are also known as what?
A. objects
B. members
C. data
D. objects & data

Show Answer Answer:-B. members
Explanation Variables declared inside a class are called as data elements or data members.

Q 2. What will be used when terminating a structure?
A. :
B. }
C. ;
D. ;;

Show Answer Answer:-C. ;
Explanation While terminating a structure, a semicolon is used to end this up.

Q 3. What will happen when the structure is declared?
A. it will not allocate any memory
B. it will allocate the memory
C. it will be declared and initialized
D. it will be declared

Show Answer Answer:-A. it will not allocate any memory
Explanation While the structure is declared, it will not be initialized, So it will not allocate any memory.

Q 4. The declaration of the structure is also called as?
A. structure creator
B. structure specifier
C. structure signifier
D. structure creator & signifier

Show Answer Answer:-B. structure specifier
Explanation The structure declaration with open and close braces and with a semicolon is also called structure specifier.

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

#include <iostream>

#include <string.h>

using namespace std;

int main()

{

struct student

{

int num;

char name[25];

};

student stu;

stu.num = 123;

strcpy(stu.name, “John”);

cout << stu.num << endl;

cout << stu.name << endl;

return 0;

}

A.123
john

B. john
john

C. compile time error
D. runtime error

Show Answer Answer:-A.123 john
Explanation We are copying the value john to the name and then we are printing the values that are in the program. Output: $ g++ stu.cpp $ a.out 123 john

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

#include <iostream>

using namespace std;

struct Time

{

int hours;

int minutes;

int seconds;

};

int toSeconds(Time now);

int main()

{

Time t;

t.hours = 5;

t.minutes = 30;

t.seconds = 45;

cout << “Total seconds: ” << toSeconds(t) << endl;

return 0;

}

int toSeconds(Time now)

{

return 3600 * now.hours + 60 * now.minutes + now.seconds;

}

A. 15000
B. 20000
C. 19845
D. 19844

Show Answer Answer:-C. 19845
Explanation In this program, we are just converting the given hours and minutes into seconds. Output: $ g++ stu1.cpp $ a.out Total seconds:19845

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

#include <iostream>

using namespace std;

int main()

{

struct ShoeType

{

string style;

double price;

};

ShoeType shoe1, shoe2;

shoe1.style = “Adidas”;

shoe1.price = 9.99;

cout << shoe1.style << ” $ “<< shoe1.price;

shoe2 = shoe1;

shoe2.price = shoe2.price / 9;

cout << shoe2.style << ” $ “<< shoe2.price;

return 0;

}

A. Adidas $ 9.99Adidas $ 11.11
B. Adidas $ 9.99Adidas $ 9.11
C. Adidas $ 9.99Adidas $ 1.11
D. Adidas $ 11.11Adidas $ 11.11

Show Answer Answer:-C. Adidas $ 9.99Adidas $ 1.11
Explanation We copied the value of shoe1 into shoe2 and divide the shoe2 value by 9, So this is the output. Output: $ g++ stu2.cpp $ a.out Adidas $ 9.99 Adidas $ 1.11

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

#include <iostream>

using namespace std;

struct sec

{

int a;

char b;

};

int main()

{

struct sec s ={25,50};

struct sec *ps =(struct sec *)&s;

cout << ps->a << ps->b;

return 0;

}

A. 252
B. 253
C. 254
D. 262

Show Answer Answer:-A. 252
Explanation In this program, We are dividing the values of a and b, printing it. Output: $ g++ stu5.cpp $ a.out 252

Q 9. Which of the following is a properly defined structure?
A. struct {int a;}
B. struct a_struct {int a;};
C. struct a_struct int a;
D. struct a_struct {int a;}

Show Answer Answer:-B. struct a_struct {int a;};
Explanation option struct {int a;} is not correct because name of structure and ;(after declaration) are missing. In option struct a_struct {int a;} ; is missing. In option struct a_struct int a; {} are missing.

Q 10. Which of the following accesses a variable in structure *b?
A. b-var;
B. b.var;
C. b->var;
D. b>var;

Show Answer Answer:-C. b->var;
Explanation Because arrow operator(->) is used to access members of structure pointer whereas dot operator(.) is used to access normal structure variables.

Comments

Leave a Reply

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

You cannot copy content of this page