Click the Start button when you are ready to begin the exam, but only then as you can only take the exam once. Click on the Next button to go to the next question. Click on the Previous button to go to the previous question. Use the number buttons to jump to a particular question. Click the Pause button to pause the exam (you will not be able to see the questions when the exam is paused). Click on the Finish button after you have answered all the questions. The number correct, number wrong, and number skipped will be displayed.
Assume that list has been initialized with the following Integer objects: [9, 3, 17, 2, 16, 4, 1]. Which of the following shows the values in list after a call of mystery(4)?
This would be true if the code reversed the list. But does it? Remember that remove(0) removes the first item in the list and returns it. The add method adds the item to the end of the list.
public class Dog
{
private String name;
public void setName(String n)
{
name = n;
}
public String getName()
{
return name;
}
}
public class DogOwner
{
private String name;
private Dog[] dogs;
}
Overloading is when a class has two methods with the same name but the parameter lists are different. It is not a type of relationship between classes.
Class C extends class B, which extends class A. Also, all of the three classes implement a public method test(). How can a method in an object of class C invoke the test() method defined in class A (without creating a new instance of class A)?
This would run the test method in class C since the object was created by the C class. When a method is called the runtime system will start looking for the method in the class that created the object.
You can use super to force the runtime to run a method in a parent class, but there is no way to force a call to a method in a grandparent (parent of your parent) class.
public class Vehicle
{
public void test(Car x, SportsCar y) {}
}
public class Car extends Vehicle {}
public class SportsCar extends Car {}
public class VehicleTest
{
public static void main(String[] args)
{
Vehicle v = new Vehicle();
Car c = new Car();
SportsCar sporty = new SportsCar();
}
}
The test method takes a Car object and a SportsCar object. Only this answer correctly passes a SportsCar object as the second parameter. You can use a SportsCar object as a Car object since it is a subclass of Car. The test method can be called on any child of Vehicle.
This would be true if the call was recur(2). This would return recur(1) + recur(0). Both recur(1) and recur(0) would return 1 so recur(2) would return 1 + 1 = 2.
This would be true if the call was recur(4). This would return recur(3) + recur(2). The call recur(3) returns 3. The call recur(2) returns 2. So recur(4) returns 3 + 2 = 5.
This would be true if the call was recur(3). This would return recur(2) + recur(1). The call to recur(1) would return 1. The call to recur(2) would return recur(1) + recur(0). Both recur(1) and recur(0) would return 1 so recur(2) would return 1 + 1 = 2. Thus recur(3) would return 2 + 1 = 3.
This code adds "a" to the end of the list: ["a"] and then "b" to the end of the list: ["a", "b"]. Then it changes the value at index 1 to "c": ["a", "c"]. Then it adds "d" at position 2 which first moves to the right any existing values ["a", "c", "d"]. Then it sets the value at index 2 to "e": ["a", "c", "e"]. Then it adds "g" to the end: ["a", "c", "e", "g"].
public class Parent
{
public void m1()
{
System.out.print("pm1");
m2();
}
public void m2()
{
System.out.print("pm2");
}
}
public class Child extends Parent
{
public void m1()
{
super.m1();
System.out.print("cm1");
}
public void m2()
{
super.m2();
System.out.print("cm2");
}
}
When p.m1() is run it will execute the m1 method in Child since p is an object of that class. The first line calls super.m1() which will execute the m1 method in Parent. That method will print "pm1" and then call m2(). The m2 method in child will execute since p is a Child object. The first line in that method calls super.m2() which will execute the m2 method in Parent. This will print "pm2". Then the parent m2 method will return, so execution will continue in the m2 method of Child and it will print "cm2". Then the child m2 method will return which will continue execution in the m1 method of Child which will print "cm1".
This would be true if p was an object of the Parent class, but it is an object of the Child class and the runtime will start execution of a method in the Child class method if it has it.
Remember that each method call is added to the call stack and after the method returns execution continues with the next statement after the method call.
public class Animal
{
// constructors not shown
public void eat()
{ // code not shown
}
}
public class Bear extends Animal
{
// constructors not shown
public void growl()
{ // code not shown
}
}
Assume that the following declaration is in a different class.
Animal b = new Bear();
Which of the following will compile without error?
I. b.eat();
II. b.growl;
III. ((Bear) b).growl();
I works since the declared type is Animal and Animal has an eat method. III works because the cast tells the compiler to treat b is a Bear and Bear has a growl method.
This loops through the top half rows (height / 2) and mirrors the values from the bottom half p[row][col] = p[height - row - 1][col]; So p[0][0] = p[height - 1][0] and p[0][1] = p[height - 1][1].
Mirrors the values from the right half to the left half of the 2D array
This method prints the right most digit (x % 10 returns the right most digit) and then if x / 10 is not equal to 0 (x < 10) it returns mystery of the current number after chopping off the right most digit. So mystery(4321) prints 1 and then calls mystery(432) which prints 2 and then calls mystery(43) which prints 3 and then calls mystery (4) which prints 4. Since 4 / 10 is equal to 0 it wonβt do a recursive call. It prints 4 again and mystery(4) returns. Execution will return to mystery(43) after the recursive call to mystery(4) and the 3 will print and then mystery (43) will return. Execution will return to mystery(432) after the recursive call to mystery(43) and the 2 will print and then mystery (432) will return. Execution will return to mystery(4321) after the recursive call to mystery(432) and the 1 will print and then mystery (4321) will return.
A classroom is a room and a building has many rooms. If the three classes Room, Classroom, and Building create objects that have the same relationship which of the following is the most appropriate set of declarations?
If a classroom is a room, then Classroom should extend Room (inherit from it). If a Building has rooms it should have a field that holds them. Since a Building can have more than one Room we can use an array to hold the rooms.
public class Room extends Building { private Classroom room; β¦. }
This prints j * k and for each value of j from 1 to 5, k changes from 1 to 2. So when j is 1 it will print 1 2. When j is 2 it will print 2 4. When j is 3 it will print 3 6. When j is 4 it will print 4 8. When j is 5 it will print 5 10.
public void sample(int num1, int num2)
{
int result = 99;
if (num1==num2)
{
result = 0;
}
else if (num1>num2)
{
result = 1;
}
else {
result = -1;
}
System.out.println(result);
}
public void method1(int num1, int num2)
{
int result=99;
if (num1 == num2)
{
result = 0;
}
else {
if(num1 > num2)
{
result = 1;
}
else
{
result = -1;
}
}
System.out.println(result);
}
public void method2(int num1, int num2)
{
int result = 99;
if (num1 == num2)
{
result = 0;
}
if (num1 >= num2)
{
result = 1;
}
else
{
result = -1;
}
System.out.println(result);
}
public void method3(int num1, int num2)
{
int result = 99 ;
if (num1 == num2)
{
result = 0;
}
if (num1 > num2)
{
result = 1;
}
if (num1 < num2)
{
result = -1;
}
System.out.println(result);
}
This one wonβt work. The problem with method2 is that if num1==num2 the first if will execute, but so will the second if and result will be set to 1.
While method3 will work, method2 wonβt. The problem with method2 is that if num1==num2 the first if will execute, but so will the second if and result will be set to 1.
String s1 = new String("hi");
String s2 = new String("hi");
String s3 = s2;
Which of the following would return true:
I. s1.equals(s2)
II. s1 == s2
III. s2.equals(s3);
IV. s2 == s3;