In: Computer Science
Question 31
Given the code snippet below, what prints?
void fun(int *p)
{
int q = 10;
p = &q;
}
int main()
{
int r = 20;
int *p = &r;
fun(p);
cout << *p;
return 0;
}
Question 31 options:
10 |
|
20 |
|
compiler error |
|
Runtime error |
Question 32
A union’s members are exactly like the members of a Structure.
Question 32 options:
True | |
False |
Question 33
Given the code below, what are the errors.
#include <iostream>
using namespace std;
int main()
{
int x, *p;
x=15;
ind_square(*p);
}
ind_square(int * p)
{
*p = *=p **p;
}
Question 33 options:
Undeclared identifier or function ind_square |
|
“*=” this expects an expression |
|
No type specifier for ind_square |
|
All of the above |
Question 34
Given the structure definition below, a member of the structure can be accessed by one of the following statements:
struct Employee{
string emName;
string emSex;
string emAddress;
int emSalary;
};
Employee Emp;
Question 34 options:
Employee.emName; |
|
Emp.emName; |
|
Emp->emName; |
Question 35
Using the structure below, which of the following statements
about creating an array (size 20) of structures are not true?
struct Employee{
string emp_id;
string emp_name;
string emp_sex;
};
Question 35 options:
Employee emp[20]; |
|
Empoyee[] emp=new Employee[20]; |
|
Employee[20] emp; |
Question 36
What are the uses for the * operator.
Question 36 options:
Multiplication |
|
Pointer declaration |
|
Deallocation |
Question 37
Question 37 options:What would be printed from the following C++ program?
#include <iostream>
#include <string>
using namespace std;
void printinfo(int);
void printinfo(string);
struct student //define structure student
{//variables
int id;
string name;
};
int main()
{
student st; //structure object
st.id = 100; //assign 100 to student id
st.name = "Sok";//assign Sok to student name
//output student information
printinfo(st.id);
printinfo(st.name);;
return 0;
}
void printinfo(int id){
cout<<id<<"\n";
}
void printinfo(string name)
{
cout<<name<<"\n";
}
Question 38
Which of the following statements about the structure is not true?
Question 38 options:
To use a structure, previously it is defined. |
|
A structure can have different types of variables in it. |
|
A structure can't have arrays in it. |
|
All of the above |
Question 39
The _____i s the name of the structure, and the _____ declared inside the braces of the structure are called the ______ of the structures.
Question 40
Use the correct function to print the length of the txt string.
string txt = “Hello”;
cout <<
;
Question 31
Answer: 20
Pointers are also passed by value. So when p is passed to fun what ever changes made in fun is not affected in main function, it will still points to the address &r.
Question 32
False
Union members share share a single memory location, while structure members have individually allocated spaces.
Question 33
All of the above
Question 34
Emp.emName;
Question 35
Empoyee[] emp=new Employee[20];
Employee[20] emp;
Are false
Question 36
Multiplication
Pointer Declaration
Though it's vauguely any use in pointer declaration, * is still used. Other use for * is pointer dereferencing.
deallocation is freeing the memory spaces which is done by delete operator or free function.
Question 37
Output
100
Sok
Question 38
A structure can't have arrays in it.
both statements
To use a structure, previously it is defined.
A structure can have different types of variables in it.
are true
Question 39
The tag is the name of the structure, and the variables declared inside the braces of the structure are called the members of the structures.
Question 40
We use length() member function String object.
Like this
string txt =
“Hello”;
cout << txt.length();