Here is a listing of C++ interview questions on “Structures” along with answers, explanations and/or solutions:
1. The data elements in the structure are also known as what?
a) objects
b) members
c) data
d) objects & data
Answer: b
Clarification: Variables declared inside a class are called as data elements or data members.
2. What will be used when terminating a structure?
a) :
b) }
c) ;
d) ;;
Answer: c
Clarification: While terminating a structure, a semicolon is used to end this up.
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
Answer: a
Clarification: While the structure is declared, it will not be initialized, So it will not allocate any memory.
4. The declaration of the structure is also called as?
a) structure creator
b) structure signifier
c) structure specifier
d) structure creator & signifier
Answer: c
Clarification: The structure declaration with open and close braces and with a semicolon is also called structure specifier.
5. What will be the output of the following C++ code?
-
#include -
#include -
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
Answer: a
Clarification: 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
6. What will be the output of the following C++ code?
-
#include -
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) 19845
b) 20000
c) 15000
d) 19844
Answer: a
Clarification: In this program, we are just converting the given hours and minutes into seconds.
Output:
$ g++ stu1.cpp $ a.out Total seconds:19845
