Card 0 of 374
What's wrong with the following code?
void printsometext()
{
cout<<"printing text
";
for(int i=0;i<3;i=i+1)
{
cout<<i<<"
"
}
return 2;
}
It is very important when creating a function to select the return type. If you want your function to return an integer, you would place "int" before your function to signify that the function expects an integer output. In this case, the function is declared as void, meaning there is no output. However, the code indicated a return value of 2, which conflicts with the void keyword. To fix this problem, either remove the return statement or change void to int.
Compare your answer with the correct one above
What is the difference between declaring a function as void and declaring it as int?
Function delcarations are very significant, as you must define what the return type of the function is. A void function doesn't need a return type, whereas the int function must have an integer value as its return type.
Compare your answer with the correct one above
Given the following initialized array:
int fourth;
int\[\]\[\] myArray = { {1, 2, 3},
{4, 5, 6},
{7, 8, 9} };
Using myArray, how can I store in variable "fourth", the number 4?
When a two dimensional array is created and initialized, the way to access the items inside the matrix is by calling the array with the row and column (i.e. myArray\[ROW\]\[COLUMN\]). Keeping in mind that arrays start at 0, the number four would be in row 1, column 0. Therefore to save that number into the variable "fourth" we'll do the following:
fourth = myArray\[1\]\[0\];
*Note: myArray\[1\]\[0\] is not the same as myArray\[0\]\[1\].
myArray\[1\]\[0\] =4 because it is the item located at row=1 and column = 0.
myArray\[0\]\[1\] =12 because it is the item located at row=0 and column = 1.
Compare your answer with the correct one above
Study the following pseudocode.
**int * var1;**
**int foo = 63;**
**var1 = &foo;**
**var2 = *var1;**
What are the values of var1 and var2?
Pointers store the address of another variable. Pointers are declared by naming the type, then an asterisk, followed by the name of the variable. In our example, var1 was declared a pointer that points to an integer:
int * var1;
Next in the code, we see that an integer variable named "foo" is created and is assigned a value of 63.
The address-of operator (&) is then used to get the address of a variable. Now lets take a look at the next line of the code:
var1 = &foo;
Here the ampersand is used to get the address of foo within memory. This means that var1 contains the address value of where foo is stored in memory.
Next in the code, we have the following statement:
**var2 = *var1;**
Here the dereference operator (*) is being used. This operator is used whenever we want to get the value (not the address) of the variable that a pointer is pointing to. In this case, var1 is storing the address of foo. However, by using the dereference operator we can get the actual value of the variable whose address is being stored in var1. In this case, dereferencing var1, we get the actual value of foo which is 63. Therefore, var2 = 63.
Compare your answer with the correct one above
Imagine that I want to write a code for a bank that asks a user whether he/she wants to make a withdrawal, make a deposit, or quit. What type of loop would be best to use in order to make sure the user is able to do as many transactions as they want until they press quit to end the program?
In our problem, the program has to run AT LEAST ONCE in order to present all of the options to the user. Because of this reason, the best option is a do-while loop because the statements within that loop execute at least one time and the condition to get out of the loop is at the end. In this case, the exit condition would be if the user presses quit (after the options have been shown at least once).
A while loop might work in this case; however it is not necessarily the smartest option. This is because the condition is tested before anything is executed within the loop. This means that whatever is inside the while loop does not necessarily have to be executed at all.
A for loop is also not a good option for this problem because a for loop is executed a set amount of times. In this case, we don't know if the user is going to want to do 1 transaction or 10. Therefore, a for loop is not the best choice.
An infinite loop is also not what we want because it runs forever. In our case, we want the code to stop prompting the user and end after "quit."
Compare your answer with the correct one above
What would be the best data structure for a library? The data is in the form of a title and a number of copies of the title.
Hash map - key being title and value being the number of copies
Hash maps are a collection of (key, value) pairs.
Hash maps have O(1) access, so this would be the quickest and best way to store the data.
Compare your answer with the correct one above
True or False.
The best data structure to represent a set of keys and values is an array.
Arrays can be two-dimensional. However, when trying to keep track of keys and values it can become complicated when using an array. HashMaps are the best way to represent data containing keys and values.
Compare your answer with the correct one above
What is not a feature of the Java programming language?
In Java, the only way to write functions is to make them class methods. Java does have primitive types int, boolean double. There is compile time error checking. Java is an Object Oriented langauge and supports the OO paradigm. There is automatic garbage collection support, which helps manage memory for the user.
Compare your answer with the correct one above
Consider the history of the following popular programming languages:
PHP
Java
Objective-C
Python
Which of the following is the closest ancestor shared by ALL of these languages?
All of these languages are C-based languages.
A clue was the answer "Objective-C," which is a strict superset of C that adds Object Orientation.
Compare your answer with the correct one above
What is the value of the string kitchen after the following code is run?
The constructor here in line 4 of the class definition is where it gets tricky. In the initialization of the constructor, we note that the input is a string.
Going down to line 10, to where the constructor function is defined, we see that a constructor with an input of c, which is defined as a string, will set the value of kitchen to c.
Finally, going down to our main code, we see that the value of the constructor in main is 'big', defined in str.
So kitchen='big'.
Compare your answer with the correct one above
Consider the code below:
private static class Philosopher {
private String name;
private String favoriteSubject;
public Philosopher(String n, String f) {
name = n;
favoriteSubject = f;
}
public String getName() {
return name;
}
public String getFavoriteSubject() {
return favoriteSubject;
}
public void speak() {
System.out.println("Hello, World! My name is "+name + ". My favorite subject is "+favoriteSubject);
}
}
private static class Nominalist extends Philosopher {
boolean franciscan;
public Nominalist(String n,boolean frank) {
super(n,"logic");
franciscan = frank;
}
public void speak() {
super.speak();
if(franciscan) {
System.out.println("I am a Franciscan");
} else {
System.out.println("I am not a Franciscan");
}
}
public String whoMightHaveTaughtMe() {
if(franciscan) {
return "Perhaps William of Ockham?....";
} else {
return "Perhaps it was Durandus of St. Pourçain — scandalous, a Dominican nominalist!";
}
}
}
If you wished to make a toString() method for both Philosopher and Nominalist, using the same output as in each class' current speak () methods, what would be the best way to organize your code?
I. Have toString () invoke both classes' speak() methods.
II. Have the speak() method invoke the new toString () method.
III. Create a new subclass and write the toString() method in that.
The toString() method must return a string value. Therefore, it makes the most sense that you would have this method generate the output that you currently generate in speak(). Then, you could have speak() invoke the toString() method to output the same string data.
Compare your answer with the correct one above
Given a class Thing and the code:
Thing thingOne, thingTwo;
What function call is the following equivalent to?
thingTwo = thingOne;
What's given to us is that thingOne and thingTwo have already been created, this is a vital piece of information. Let's go through each of the choices
operator=(thingTwo, thingOne);
This line of code makes no sense and is syntactically wrong.
ostream& Thing::operator=(const Thing& rhs);
This line of code says to access the "operator=" method from the "Thing" class and put it onto the ostream, this has nothing to do with assigning thingOne to thingTwo.
When we come across the choice of the copy constructor, we have to keep in mind that the copy constructor is only used when an object use to to create another one, for example we have a given class called Foo:
Foo foo;
Foo foobar = foo;
In this case, we are using foo to create foobar.
The only choice that works is:
thingTwo.operator=(thingOne);
This line of code means that the thingOne object is being assigned to thingTwo.
Compare your answer with the correct one above
For this question, consider the following code:
private static class Philosopher {
private String name;
private String favoriteSubject;
public Philosopher(String n, String f) {
name = n;
favoriteSubject = f;
}
public String getName() {
return name;
}
public String getFavoriteSubject() {
return favoriteSubject;
}
public void speak() {
System.out.println("Hello, World! My name is "+name + ". My favorite subject is "+favoriteSubject);
}
}
private static class Nominalist extends Philosopher {
boolean franciscan;
public Nominalist(String n,boolean frank) {
super(n,"logic");
franciscan = frank;
}
public void speak() {
super.speak();
if(franciscan) {
System.out.println("I am a Franciscan");
} else {
System.out.println("I am not a Franciscan");
}
}
public String whoMightHaveTaughtMe() {
if(franciscan) {
return "Perhaps William of Ockham?....";
} else {
return "Perhaps it was Durandus of St. Pourçain — scandalous, a Dominican nominalist!";
}
}
}
public static void main(String\[\] args) {
Philosopher p = new Nominalist("Nicanor",false);
System.out.println(p.whoMightHaveTaughtMe());
}
What is the output for this main method?
When you assign a subclass (like Nominalist) to a super class (like Philosopher) you will not be able to call any of the subclass-specific methods on the variable in question. This code does just that, which will prevent the code from compiling.
Compare your answer with the correct one above
Consider the following code:
private static class Philosopher {
private String name;
private String favoriteSubject;
public Philosopher(String n, String f) {
name = n;
favoriteSubject = f;
}
public String getName() {
return name;
}
public String getFavoriteSubject() {
return favoriteSubject;
}
public void speak() {
System.out.println("Hello, World! My name is "+name + ". My favorite subject is "+favoriteSubject);
}
}
private static class Nominalist extends Philosopher {
boolean franciscan;
public Nominalist(String n,boolean frank) {
super(n,"logic");
franciscan = frank;
}
public void speak() {
super.speak();
if(franciscan) {
System.out.println("I am a Franciscan");
} else {
System.out.println("I am not a Franciscan");
}
}
public String whoMightHaveTaughtMe() {
if(franciscan) {
return "Perhaps William of Ockham?....";
} else {
return "Perhaps it was Durandus of St. Pourçain — scandalous, a Dominican nominalist!";
}
}
}
public static void main(String\[\] args) {
Philosopher\[\] phils = {
new Philosopher("Petrus","Ethics"),
new Nominalist("Minimus Maximus",false),
new Nominalist("Theodoric",true)};
for(int i = 0; i < phils.length; i++) {
phils\[i\].speak();
}
}
What is the output for the code above?
Three things must be kept in mind for this question. First (and foremost), given the way that inheritance works, you know that for each object, you will get an output that matches the given object's particular type—even though the array is an array of the superclass type (namely Philosopher). Second, you must notice that the lines "I am a Franciscan" and "I am not a Franciscan" come a line after the data before it (which is printed by using the super call). Finally, be careful about punctuation! There are periods missing in some of the output lines.
Compare your answer with the correct one above
Consider the following code:
public class Rectangle {
private double width, height;
public Rectangle(double w,double h) {
width = w;
height = h;
}
``
public double getArea() {
return width * height;
}
``
public double getPerimeter() {
return 2 * width + 2 * height;
}
}
``
public class Square {
private double side;
public Square(double s) {
side = s;
}
public double getArea() {
return side * side;
}
public double getPerimeter() {
return 4 * side;
}
}
Which of the following represents a redefinition of Square that utilizes the benefits of inheritance?
We know that a square really is just a subclass of a rectangle, for it is merely a rectangle having four sides that are all equal. Using inheritance, you can very easily reuse much of your Rectangle
code. First, you need to extend the Rectangle
class:
public class Square extends Rectangle { . . .
Next, you can be rid of the field side
. This allows you to alter the constructor for Square
to call the Rectangle
constructor. You do this using super
(because Rectangle is the superclass).
After this, you can delete the getArea
and getPerimeter
methods, for they will be handled by the superclass. This gives you a very simple bit of code!
Compare your answer with the correct one above
Which of the following is TRUE about the Object class?
Object is the most basic class which all others, even user made ones, inherit. Therefore, all classes have Object's methods. A way to think of classes is to think of a tree: the Object class is the lowest node on the tree, where all other nodes can connect back to.
Compare your answer with the correct one above
class Z
{
public:
void Func4();
};
class Y : public Z
{
public:
virtual void Func3();
};
class B : public Y
public:
virtual void Func1();
void Func2();
};
class C : public B
public:
virtual void Func1();
};
What is the base class in the above code.
The base class is a class that doesn't derivate from any other class. Starting from the bottom of the code, we see that class C is inherited from class B, which is inherited from class Y, which is finally inherited by class Z. Class Z is the base class.
Compare your answer with the correct one above
class Pet {
public:
Pet() {}
virtual void bar() {cout << "In pet bar(); }
};
class Cat : public Pet {
public:
virtual void eat() {cout << "Cat eating"; }
virtual void bar() {cout << "In Cat bar()"; }
};
Given the above classes, what would the result of:
int main(){
Pet * petPtr = new Cat();
petPtr -> eat();
}
When a child class inherits the properties of a parent class, some attributes are inherited and some aren't. In C++, the constructor of the parent class is not inherited. The Cat class does not have a constructor therefore the program will not compile.
Compare your answer with the correct one above
class Pet {
public:
Pet() {}
virtual void bar() {cout << "In pet bar(); }
};
class Cat : public Pet {
public:
virtual void eat() {cout << "Cat eating"; }
virtual void bar() {cout << "In Cat bar()"; }
};
Given the above classes, what would the result of:
int main(){
Cat felix;
Pet peeve;
peeve = felix;
peeve.bar();
}
The Cat class is inherited from the Pet class and an instance of each object is created in main.
peeve = felix;
When felix is assigned to peeve, it loses it's atributes as a Cat and now becomes a Pet. When the bar method is called, it will call bar inside the Pet class and not the Cat class.
If the line of code was:
felix = peeve;
Then, the program will crash. This crashes because we are trying to assign a Pet (which is the parent), to a Cat (which is a child class). Assigning a parent to a child will cause a Slicing Problem because the parent is "bigger" than the child. If you try to put something big (a Pet) into a small container (a Cat), it will not work.
Compare your answer with the correct one above
class Member{
public:
Member() { cout<< 1 ; }
};
class Base{
public:
Base(){ cout<< 1; }
Memeber member;
};
class Derived : public Base{
public:
Derived(){ cout << 3; }
};
int main(){
Derived der;
}
Given the code above, what is the output of the program?
When an object is created the order of which everything is created is as follows:
1. Member variables are created first.
2. The constructor of the parent class called (if the object has a parent)
3. The object's own constructor is called.
Following the code, we can see that a Derived object is created in main. Since the Derived class has a parent, the call order jumps to the Base class. Now we are inside the Base class, nothing has been outputted yet. Inside the Base class, the local variables are created first. This brings us to the Member class.
In the Member class, the constructor is called and 1 is outputted.
After this, we jump back to the constructor of the Base class and 2 is outputted.
Finally, we get back to where we started, the Derived class. The constructor of derived is called and 3 is outputted. The final output is 123.
Compare your answer with the correct one above