Card 0 of 20
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
True or False.
The class BetterMan inherits from the class Man.
public class BetterMan extends Man {
}
The class BetterMan inherits methods from the class man. The keyword "extends" means that BetterMan will get all the methods from Man plus be able to extend the class by adding its own methods. All methods from Man can be used in BetterMan by calling the keyword better.
Compare your answer with the correct one above
What does the code print?
class
Parent{
final
public
void
show() {
`` System.out.println(
"Parent::show() called"
);
`` }
}
``
class
Child
extends
Parent {
`` public
void
show() {
`` System.out.println(
"Child::show() called"
);
`` }
}
``
public
class
Main {
`` public
static
void
main(String[] args) {
Parent parent
=
new
Child();
parent
.show();
`` }
}
Final methods can't be overriden. So the code won't compile because of this. Now if the final modifier were to be removed. The code would compile and run and produce:
Child::show()
Compare your answer with the correct one above
Which of the following header statements allow you to omit using the std:: when using cout?
You must put using namespace std; at the top of your file to avoid having to type std:: every time you use cout and cin.
Compare your answer with the correct one above
Which of the access modifiers don't work in Java?
One of the benefits to Java is that there is garbage collection. So when data is disposed of its done automatically versus a language like C++ where you need to tell the program to delete dynamically allocated data.
Compare your answer with the correct one above
What line of code is needed to import in order to use Color in the following format Color.Blue, Color.RED, Color.White?
If you were to use the following format Color.Red, Color.Blue, etc then the correct answer would have to be java.awt.*; While java.awt.Color; does fit the appropriate code, if you import that entire statement, you don't need to put Color.Red just Red and following. The other two import statements are just completely wrong.
Compare your answer with the correct one above
CONSIDER THE FOLLOWING JAVA CODE:
**public class Employee**
**{**
static int nextID = 1;
String name;
int employeeID;
``
Employee(String n){
name = n;
employeeID = nextID;
nextID++;
}
public String getName(){
return name;
}
public int getID(){
return employeeID;
}
public static void main(String[] args)
{
Employee emp3 = new Employee("Kate");
Employee emp1 = new Employee("John");
Employee emp2 = new Employee("Sandy");
System.out.println(emp1.getName() + " ID: " + emp1.getID());
System.out.println(emp3.getName() + " ID: " + emp3.getID());
}
**}**
WHAT WOULD BE THE CONSOLE OUTPUT?
Static variables are associated with a class, not objects of that particular class. Since we are using a static class variable "nextID" and assigning that to employeeID as a new employee is created and then increasing it, the class variable is updated for the whole class.
In our code, we have emp3 being created first. This means that Kate is created with an employeeID equaling the current value of nextID (which is 1 in this case). Afterwards, the nextID value increases by 1. Next emp1 is created with the name of John and an employeeID equaling to the current value of nextID (which became 2 due to the increased that occurred when Kate was created). Lastly, emp2 is created with the name of Sandy and an employeeID equaling the current value (at that point in the code) of nextID. Meaning Sandy has an employeID value of 3.
Therefore in the first System.out. print statement when we print emp1, we get: John ID: 2
and in the second System.out. print statement when we print emp3 we get:
Kate ID: 1
Compare your answer with the correct one above
What is the relationship between the class Car and the class TwoDoor in the code snippet below?
class TwoDoor extends Car {
}
Car is the super class to TwoDoor. Since the class TwoDoor extends the class Car, TwoDoor can use methods and variables from Car by calling super.
The answers that mention implements are incorrect because implementing refers to interfaces. For TwoDoor to implement car, the code would look like this:
class TwoDoor implements Car {
}
Compare your answer with the correct one above
Describe the difference between overloading a method and overriding a method.
When you override a method, you are completely replacing the functionality of the method by overshadowing the method in the parent child with the one in the child class.
Overloading a method just allows you to have the same method name to have multiple meanings or uses. An example is '+' operator. In languages like Java, you can use '+' to append strings, add doubles, or add integers.
Compare your answer with the correct one above
What is the difference between extending and implementing?
By default, all classes are extensions of the Object class and therefore are extending a single class. Due to that concept, implementation was made with the idea that notion of only being able to extend a single class. Thus they allow you to implement an infinite amount of classes.
Compare your answer with the correct one above
Which of the following lines represents a data member?
1. class animal
2. {
3. public:
4. animal();
5. void fetch();
6. private:
7. char bone;
8. }
Data members are variables created in a class, and are typically found under the private: label, as data members are typically kept local to that class. It can be distinguished from member functions because of its lack of parenthesis().
Compare your answer with the correct one above
Which of the following operations allow you to define the function func1() outside of the class definition?
1. class school
2. {
3. public:
4. school();
5. void func1();
6. private:
7. char class;
8. }
The function template for func1() has been defined in the class definition, however the implementation of the function has not. In order to specify what the function does, you must say
classname::functionname()
{
}
Or, in our case,
school::function1()
{
//function definition goes in here
}
Compare your answer with the correct one above
Design a shell for a class named Dog that has 3 attributes:
In the prompt, any yes or no questions should be addressed as booleans (i.e. tail, purebred). Any lists should have the type and name of the list. This was just a shell, so there should be no declarations of variables.
Compare your answer with the correct one above