Loading...

C++ Multiple Choice Questions

Our C++ questions and answers focuses on all areas of C++ programming language covering 100+ topics in C++

C++ Structures MCQ | Set 1

C++ Structures | Set 1


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

a) objects
b) members
c) data
d) objects & data



2. What will be used when terminating a structure?

a) :
b) }
c) ;
d) ;;



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



4. The declaration of the structure is also called as?

a) structure creator
b) structure signifier
c) structure specifier
d) structure creator & signifier



5. What will be the output of the following C++ code?
#include <iostream>
#include <string.h>
using namespace std;
int main() {
    int student{
        int num;
        char name[25];
    }
    student stu;
    stu.num = 123;
    strcpy(stu.num, "john");
    cout << stu.num << endl;
    cout << stu.name << endl;

    return 0;
}

a) 123
john
b) john
john
c) compile time error
d) runtime error



6. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
struct Time{
    int hours, minutes, 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) 19845
b) 20000
c) 15000
d) 19844



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 $ 1.11
b) Adidas $ 9.99Adidas $ 9.11
c) Adidas $ 9.99Adidas $ 11.11
d) Adidas $ 11.11Adidas $ 11.11



8. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
struct sec{
    int a;
    char b;
};
int toSeconds(Time now);
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



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



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

a) b->var;
b) b.var;
c) b-var;
d) b>var;



- Related Topics