Hi folks, please help me with these 15 multiple choice questions. Consider the f
ID: 661098 • Letter: H
Question
Hi folks, please help me with these 15 multiple choice questions.
Consider the following non-member function:
int addition (int a, int b)
{ int r;
r = a + b;
return r;
}
Which of the following could replace the above function with a member function of the class named myclass and properly call the member function?
Select one:
int myclass::addition (int a, int b)
{ int r;
r = a + b;
return r;
}
int main()
{
int num1 = 1;
int num2 = 2;
int total = 0;
myclass test1;
total = test1.addition(num1, num2);
}
int myclass:addition (int a, int b)
{ int r;
r = a + b;
return r;
}
int main()
{
int num1 = 1;
int num2 = 2;
int total = 0;
myclass test1;
total = myclass.addition(num1, num2);
}
int myclass.addition (int a, int b)
{ int r;
r = a + b;
return r;
}
int main()
{
int num1 = 1;
int num2 = 2;
int total = 0;
myclass test1;
total = test1::addition(num1, num2);
}
int addition::myclass (int a, int b)
{ int r;
r = a + b;
return r;
}
int main()
{
int num1 = 1;
int num2 = 2;
int total = 0;
myclass test1;
total = myclass.addition(num1, num2);
}
Which statement initializes (allocates) a pointer to point to a memory address in the heap?
Select one:
int* p;
int *p = new int;
int p = new int;
int& p = new int;
Here is a function declaration:
void goo(int* x)
{
*x = 1;
}
Suppose that a is an int* variable pointing to some integer, and *a is equal to zero. What is printed if you cout << a; after the function call goo(a)?
Select one:
The address of x
1
0
The address of a
What is printed by these statements?
int *p1 = new int;
int *p2 = p1;
*p1 = 3;
*p2 = *p1;
cout << p1 << endl;
Select one:
4
The address of p1
3
Unpredictable
What is wrong with this block of code?
int *p1 = new int; //line 1
int *p2; //line 2
*p2 = 4; //line 3
p1 = p2; //line 4
cout << p1 << p2 << endl; //line 5
Select one:
a. A pointer assignment to set the address of a pointer cannot be done with an integer value on line 3.
b. The p1 pointer is incorrectly instantiated with the wrong operator before accessing its memory location on line 1.
c. The p2 pointer is not instantiated with the new operator before accessing its memory location on line 3.
d. Parentheses are missing from this statement.
What is the delete keyword used for?
Select one:
To delete both the pointer and to deallocate the space in the heap that is pointed to.
To deallocate the space in the heap pointed to by a pointer.
To delete the value stored in the heap pointed to by a pointer
To remove a pointer address from the stack
Which of the following declarations is legal (will not generate an error)?
Select one:
int *q;
int q = new *int;
int q* = new int;
int q = new int;
What is printed by these statements?
int p1 = 3;
int &p2 = p1;
p2 = p1;
cout << p1 << endl;
Select one:
The address in the stack of both p1 and p2
3
The address in the stack of p1
An error occurs
Consider the following class:
class hourlyEmp: public employee {
public:
hourlyEmp();
hourlyEmp(const string& newName, const string& newSsn,
double newPayRate, double newHours);
double getHours() const;
void setHours(double newHours);
void giveRaise(double amount);
void printCheck() const;
private:
double hours;
double payRate;
};
How could you create an object of this class (Select all that apply)?
Select one or more:
hourlyEmp arnold("Arnold Jones","23456664",13,20);
hourlyEmp arnold();
employee::hourlyEmp arnold("Arnold Jones");
employee arnold("Arnold Jones","23456664",13,20);
Which of the following is a pure abstract function?
Select one:
virtual double area() = 0;
pure virtual double area() = {};
virtual double area() {area= 0};
abstract double area();
A programming language is required to provide which things in order for it to be considered an object oriented programming language?
Select one or more:
Pointers
Inheritance
Encapsulation
Polymorphism
Consider the following:
class base {
public:
void vfunc() {
cout << "This is base's vfunc()." << endl;
}
};
class derived1 : public base {
public:
void vfunc() {
cout << "This is derived1's vfunc()." << endl;
}
};
int main()
{
base *p, b;
derived1 d1;
p = &b;
p -> vfunc();
// remember, this is equivalent to (*p).vfunc();
p = &d1;
p -> vfunc();
}
What is the output of this program?
Select one:
This is base's vfunc().
This is base's vfunc().
Error. Ambiguous reference.
This is base's vfunc().
This is derived1's vfunc().
This is derived1's vfunc().
This is derived1's vfunc()
Consider the following:
class base {
public:
virtual void vfunc() {
cout << "This is base's vfunc()." << endl;
}
};
class derived1 : public base {
public:
void vfunc() {
cout << "This is derived1's vfunc()." << endl;
}
};
int main()
{
base *p, b;
derived1 d1;
p = &b;
p -> vfunc();
// remember, this is equivalent to (*p).vfunc();
p = &d1;
p -> vfunc();
}
What is the output of this program?
Select one:
a. This is base's vfunc().
This is derived1's vfunc().
b. This is derived1's vfunc().
This is base's vfunc().
c. This is derived1's vfunc().
This is derived1's vfunc().
d. This is base's vfunc().
This is base's vfunc()
Consider the following code.
string str1(3, 'a');
cout << str1 << endl;
string str2(str1);
cout << str2 << endl;
string str3("hello", 2);
cout << str3 << endl;
string str5 = "How are you?";
string str6(str5, 4, 3);
cout << str6 << endl;
Assuming that this code has the using namespace std directive and the appropriate #include statements, what would you expect the output to be?
Select one:
aaa
aaa
he
are
aaa
aa
he
are
he
aaa
aaa
aaa
aaa
aa
he
are
25.Consider the following class definition:
// this is the file "employee.h"
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using namespace std;
class employee {
public:
employee();
employee(const string& newName, const string& newSsn);
string getName() const;
string getSsn() const;
void changeName(const string& newName);
void changeSsn(const string& newSsn);
protected:
string name;
string ssn;
double netPay;
};
#endif
Which of the following is a child class of the employee class shown above?
Select one:
// this is the file "contractor.h"
#ifndef CONTRACTOR_H
#define CONTRACTOR_H
#include "employee.h"
#include
using namespace std;
class contractor: public employee {
public:
contractor();
contractor(const string& newName, const string& newSsn,
double newPayRate, double newHours);
double getHours() const;
void setHours(double newHours);
void giveBonus(double amount);
void printCheck() const;
private:
double hours;
double payRate;
};
#endif
// this is the file "contractor.h"
#ifndef CONTRACTOR_H
#define CONTRACTOR_H
#include "employee.h"
#include
using namespace std;
class contractor:: public employee {
public:
contractor();
contractor(const string& newName, const string& newSsn,
double newPayRate, double newHours);
double getHours() const;
void setHours(double newHours);
void giveBonus(double amount);
void printCheck() const;
private:
double hours;
double payRate;
};
#endif
// this is the file "contractor.h"
#ifndef CONTRACTOR_H
#define CONTRACTOR_H
#include "employee.h"
#include
using namespace std;
class contractor: inherit employee {
public:
contractor();
contractor(const string& newName, const string& newSsn,
double newPayRate, double newHours);
double getHours() const;
void setHours(double newHours);
void giveBonus(double amount);
void printCheck() const;
private:
double hours;
double payRate;
};
#endif
// this is the file "contractor.h"
#ifndef CONTRACTOR_H
#define CONTRACTOR_H
#include "employee.h"
#include
using namespace std;
class employee: public contractor {
public:
contractor();
contractor(const string& newName, const string& newSsn,
double newPayRate, double newHours);
double getHours() const;
void setHours(double newHours);
void giveBonus(double amount);
void printCheck() const;
private:
double hours;
double payRate;
};
#endif
Explanation / Answer
1.
Scope resolution operator (::) is used to define a member function out the class.
Syntax: return type :: function(parameters list)
Thus the correct option is
int myclass::addition (int a, int b)
{ int r;
r = a + b;
return r;
}
int main()
{
int num1 = 1;
int num2 = 2;
int total = 0;
myclass test1;
total = test1.addition(num1, num2);
}
2.
When memory allocated using ‘new’, it returns memory location (address).
A pointer stores address of another variable. Therefore, the following statement allocates memory in heap.
Thus the correct option is
int *p = new int;
3.
If int *a is a pointer variable pointing to some integer. Cout<<*a prints value of intger that represented by a. whereas, cout<<a prints address stored in ‘a’. Therefore, after statement *x=1, the address in ‘a’ representing 1.
Thus the correct option is
“the address of x /a ”
4.
Correct option is “The address of p1”
5.
Before using a pointer, it must be initialized. i.e., memory must be allocated to pointer variable.
In the given code, p2 is not initialized before it is used in the code.
Thus the correct option, “The p2 pointer is not instantiated with the new operator before accessing its memory location on line 3.”
6.
The delete operator dynamically de allocates the memory pointed to by a pointer. But, it does not delete any value stored in the memory.
Thus, the correct option is “To de allocate the space in the heap pointed to by a pointer”
7.
Because of improper syntax, all the statements except first statement generate error.
Thus correct option is “int *q;”
8.
P1 is a general primitive integer data type, cout<<p1 prints the value stored in p1.
Thus the correct option is “3”
9.
The correct option is “ hourlyEmp arnold("Arnold Jones","23456664",13,20); “
10.
Abstract class has virtual functions with no definitions
Thus the correct option is “virtual double area() = 0;”
11.
C has pointers concept, but it is not an object oriented programming language. So, we can’t decide a language whether it is an object oriented programming language or not , by pointers.
But, a language can be considered as an object oriented programming language by “Inheritance Encapsulation Polymorphism”.
Thus the correct options are” Inheritance Encapsulation Polymorphism”.
12.
Obviously,
The correct option is
This is base's vfunc().
This is base's vfunc().
13.
The statements demonstrates different constructors of string class (defined in string header file)
Thus the correct option is
aaa
aaa
he
are
14.
The correct option is
// this is the file "contractor.h"
#ifndef CONTRACTOR_H
#define CONTRACTOR_H
#include "employee.h"
#include
using namespace std;
class contractor: public employee {
public:
contractor();
contractor(const string& newName, const string& newSsn,
double newPayRate, double newHours);
double getHours() const;
void setHours(double newHours);
void giveBonus(double amount);
void printCheck() const;
private:
double hours;
double payRate;
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.