Question: 1. Consider the following function called duplicate and the main function that calls it. What are the values for x, y and z that are
1. Consider the following function called duplicate and the main function that calls it. What are the values for x, y and z that are displayed?
void duplicate (int& a, int& b, int c)
{
a*=2;
b*=2;
c*=2;
}
int main ()
{
int x=1, y=3, z=7;
duplicate (z, y, x);
cout << "x=" << x << ", y=" << y << ", z=" << z;
return 0;
}
x = 3, y=6, z=7
x = 1, y=6, z=14
x = 3, y=6, z=14
x = 1, y=3, z=7
2. The >> operator can be used to write a line to a file.
True
False
3. An array is a collection of data elements of one or more data types.
True
False
4. The __________ function will return true if the file you wish to use is ready for input/output operations.
good()
is_good()
open()
ready()
5. Explain what this code does:
int jimmy [3][5];
declares a single dimensional array with 3 rows and 5 columns
declares a multi-dimensional array with the value 3 in the first index and the value 5 in the second index.
declares a single dimensional array with a size of 35.
declares a multi-dimensional array with 3 rows and 5 columns
6. Consider the following function prototypes:
void passbyValue(int valnum);
void passbyRef(int &refnum);
Which has a reference parameter?
void passbyRef(int &refnum);
void passbyValue(int valnum);
7. Explain two things that are wrong with this code and how to fix them:
#include
int sum(x, y)
{
std::string result;
result = x + y;
return result;
}
You cannot use the + operator with a char data type.
data types are required for x and y
return value must be int data type, not string
The prototype should be: void sum(x, y);
8. Consider the following code. What is the output?
foo.h:
1
2
3
4
5
int DoSomething(int nX, int nY)
{
return nX + nY;
}
goo.h:
1
2
3
4
5
int DoSomething(int nX, int nY)
{
return nX - nY;
}
main.cpp:
1
2
3
4
5
6
7
8
9
10 #include "foo.h"
#include "goo.h"
#include
using namespace std;
int main()
{
cout << DoSomething(4, 3);
return 0;
}
7
1
Error occurs due to naming collision
Error occurs due to missing class definition
9. By default, all variables and functions are defined in the global namespace.
True
False
10. Although the using keyword can be used outside of a function to help resolve every identifier in the file, this is not recommended. Why?
it decreases the chance of identifiers from multiple namespaces conflicting
it increase the chance of identifiers from multiple namespaces conflicting
it makes it extremely difficult to use identifiers from other namespaces, such as the Standard namespace.
it is considered bad style because it makes your code sloppier and more difficult to read.
11. What is the purpose of a constructor?
Constructors can be very useful for defining and initializing private member variables.
Constructors can be very useful for controlling access to private member variables by non-member functions.
Constructors can be very useful for handling all of the c++ code required in a program.
Constructors can be very useful for setting initial values for certain member variables.
12. Consider the following main function. What can you guess about the Line class?
#include "line.h"
int main( )
{
Line line(10.0);
}
It has a constructor that has one or more parameters
It has a constructor that has a parameter
It has a member variable with the same name as the class that can store the value 10.0
13. You have a main method with the following statements. Each statement creates an object of the Platypus class, but one sends an argument in parentheses and the other does not. In which circumstances would this be allowed?
Platypus p1("digger");
Platypus p1;
When the Platypus class has a single constructor that has a String parameter. When an object is created with a String argument, this constructor is called. When an object is created without a constructor argument, the default constructor is called.
When the Platypus class has an overloaded constructor. One constructor of this class would have no parameters. When an object is created without an argument, this constructor is called. The class also has another constructor that has a string parameter. When an object is created with a String argument, this constructor is called.
This will always produce an error. Only one constructor is allowed. Either a constructor with arguments or without arguments, but not both for the same class.
14. Assume that myList is a previously declared an initialized array. Assume that the variable length holds the total number of elements in the array. What does the following code block do?
for (int i = 0; i < length; i++){
cout << myList[i] << " ";
}
Displays the total number of elements in the array called myList followed by a space.
Displays the data stored before the first element of the array called myList up to the data element stored in the second to last element in the array, separated by a space.
Displays each data element stored in the array called myList, separated by a space.
Displays each index number of the array called myList, followed by a space, up to the last index number in the array's length.
15. Which of the following would be a correct way to call (invoke) this method?
void printArray(int array[]) {
for (int i = 0; i < length;i++) {
cout << array[i] << " ";
}
}
int result[]={3, 1, 2, 6, 4, 2};
printArray({3, 1, 2, 6, 4, 2});
int result[];
printArray(int result[]);
int result[]={3, 1, 2, 6, 4, 2};
printArray(result);
int[]{3, 1, 2, 6, 4, 2};
printArray(int[]);
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
