C++ Structure and Union

This quiz tests your knowledge of structures and unions in C++. It contains 10 questions.

Last Updated :
Discuss
Comments

Question 1

The size of a union is determined by the size of the __________

  • First member in the union

  • Last member in the union

  • Biggest member in the union

  • Sum of the sizes of all members

Question 2

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;
}
  • Adidas $ 9.99Adidas $ 1.11

  • Adidas $ 9.99Adidas $ 9.11

  • Adidas $ 9.99Adidas $ 11.11

  • Adidas $ 11.11Adidas $ 11.11

Question 3

The declaration of the structure is also called as?

  • structure creator

  • structure signifier

  • structure specifier

  • structure creator & signifier

Question 4

Which of the following comments about Union is false?


 

  • Union is a structure whose members share the same memory area 


     

  • The compiler will keep track of what type of information is currently stored

  • Only one of the members of union can be assigned a value at particular time 

  • Size allocated for Union is the size of its member needing the maximum storage


     

Question 5

What will output of the following C++ code?

# include <iostream>
# include <string.h>
using namespace std;

struct Test
{
  char str[20];
};

int main()
{
  struct Test st1, st2;
  strcpy(st1.str, "GeeksQuiz");
  st2 = st1;
  st1.str[0] = 'S';
  cout << st2.str;
  return 0;
}
  • Segmentation Fault

  • geeksquiz


     

  • GeeksQuiz

  • Compiler Error


     

Question 6

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


 

  • 252

  • 254

  • 258

  • 262

Question 7

What will be used when terminating a structure?

  • :

  • }

  • ;

  • ;;

Question 8

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

  • b->var;

  • b.var;

  • b-var;

  • b>var;

Question 9

Which of the following is a properly defined structure?

  • struct {int a;}

  • struct a_struct {int a;}

  • struct a_struct int a;

  • struct a_struct {int a;};

Question 10

What will happen when the structure is declared?

  • it will not allocate any memory

  • it will allocate the memory

  • it will be declared and initialized

  • it will be declared

Tags:

There are 10 questions to complete.

Take a part in the ongoing discussion