Vidyalelo
C++ Programming · all questions

Structures and Unions in C++
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

35

Questions

2/2

Page

Pick an option on a question to see the right answer and solution.

Which keyword is used to declare a union in C++?

Select an option to see the answer and solution.

What is the correct syntax to define a union in C++?

Select an option to see the answer and solution.

What is the output of the following code: struct Book { char title[50]; int pages; }; Book b; cout << sizeof(b); in C++?

Select an option to see the answer and solution.

What is the output of the following code: union Number { int x; float y; }; Number n; n.x = 10; cout << n.y; in C++?

Select an option to see the answer and solution.

What is a dangling pointer in C++?

Select an option to see the answer and solution.

The declaration of the structure is also called as?

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

What will be used when terminating a structure?

Select an option to see the answer and solution.

The data elements in the structure are also known as what?

Select an option to see the answer and solution.

Which of the following is a properly defined structure?

Select an option to see the answer and solution.

What will happen when the structure is declared?

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

Which of the following accesses a variable in structure *b?

Select an option to see the answer and solution.