Implement in different classes and that have the same


Question 1

In object oriented programming, every object...
(A) is an attribute of another object
(B) is an instance of a class
(C) inherits from a class
(D) has recursive methods
Which one of the above is correct? _______________________

Question 2

Select the correct statement or statements regarding object oriented programming in Java:
(A) Inheritance models the IS-A relationship, in which the objects of the subclass are also objects of the superclass.
(B) The number of methods in a superclass is always higher than in each of its subclasses.
(C) Two subclasses of the same superclass always have the same number of methods.
(One of more correct answer is possible)

Answer(s): ______________________

Question 3

Select the correct answer/s regarding visibility in Java. When an attribute (instance variable) is defined as private in a class:
(A) it can be accessed only once in the program
(B) it can be accessed and modified only from methods of that class
(C) it can be accessed and modified only from private methods of that class
(D) it can be accessed and modified only from methods of that class and its subclasses
(E) it can be accessed and modified only from methods of that class and its superclass
(F) it can be accessed but it cannot be modified
(One of more correct answer is possible)

Answer(s): ______________________

Question 4

Which of the following sentences about constructors in Java are correct?
(A) The return type void must be included in the declaration of a constructor
(B) A class can have several constructors
(C) Constructors are used to create objects (instances) of a class
(D) Constructors need to receive at least one argument
(E) Constructors are special methods that cannot be overloaded
(One of more correct answer is possible)

Answer(s): ______________________

Question 5

Given two points p1 and p2 represented in the bidimensional space by coordinates x and y, the length of the line that connects these two points (i.e. the distance between these two points) is given by the formula:

d = √(x2 - x1)2 + (y2 - y1)2

where x1 and y1 represent the coordinates of p1, and x2 and y2 represent the coordinates of p2.

The following two Java classes, Point and Line, represent bidimensional points and straight lines that connect two points.

public class Point {
private double x = 0;
private double y = 0;

public Point(int a, int b) {
x = a;
y = b;
}

public double getX() {
return x;
}

public double getY() {
return y;
}
}

public class Line {
private Point p1;
private Point p2;

public Line (Point firstPoint, Point secondPoint) {
p1 = firstPoint;
p2 = secondPoint;
}
}

We want to implement the method length() in the class Line. This method calculates the distance of the two points that are connected by a straight line. Would the following implementations of length() be correct?

NOTE: The method Math.sqrt(x) calculates the square root of x and the method Math.pow(x,2)calculates x squared.
(Please mark a "X" for the correct answer)
public double length() {
return Math.sqrt(Math.pow(p2.getX()-p1.getX(),2)
+ Math.pow(p2.getY()-p1.getY(),2));
}
Answer: YES ( ) NO ( )

public double length() {
return Math.sqrt(Math.pow(p2.getX()-p1.getY(),2)
+ Math.pow(p2.getY()-p1.getX(),2));
}
Answer: YES ( ) NO ( )

public double length() {
return Math.sqrt(Math.pow(p2.x-p1.x,2)
+ Math.pow(p2.y-p1.y,2));
}
Answer: YES ( ) NO ( )

public double length() {
return Math.sqrt(Math.pow(p2.x-p1.y,2)
+ Math.pow(p2.y-p1.x,2));
}
Answer: YES ( ) NO ( )

Question 6

Given the following class:
public class Student {
static int counter = 0;
int id;

public Student() {
id = counter;
counter++;
}

public static void main(String[] args){
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
System.out.println(s1.id + "" + s2.id + "" + s3.id);
System.out.println(s1.counter + "" + s2.counter + "" + s3.counter);
}
}

PART A) Which is the result printed on screen of executing:
System.out.println(s1.id + "" + s2.id + "" + s3.id);
(A) 000
(B) 001
(C) 012
(D) 111
(E) 123
(F) 222
(G) 234
(H) 333
Answer: __________________________

PART B) Which is the result printed on screen of executing:
System.out.println(s1.counter + "" + s2.counter + "" + s3.counter);
(A) 000
(B) 001
(C) 012
(D) 111
(E) 123
(F) 222
(G) 234
(H) 333
Answer: __________________________

Question 7

Given MyClass and YourClass classes, as shown below, what line should replace //...[Here]... in YourClass?
public class MyClass {
private int a;
public MyClass (int a) {
this.a = a;
}
}

public class YourClass extends MyClass {
private int b;
public YourClass (int a, int b) {
//...[Here]...
this.b = b;
}
}

(A) this.a = a;
(B) MyClass(a);
(C) super(a);
(D) YourClass(a);
(E) a=a;

Answer: ___________________________

Question 8

Which of the following options is an example of overloading:

(A) Two methods with the same name that are implemented in the same class, and that have the same number and type of parameters

(B) Two methods with the same name that are implemented in different classes and that have the same number and type of parameters

(C) Two methods with the same name that are implemented in the same class, and that have the same number of parameters but of different types

(D) Two methods with the same name that are implemented in different classes, and that have the same number of parameters but of different types

(E) One method implemented in one class, and another method with the same name, number and type of parameters implemented in a class that inherits from the first one

Request for Solution File

Ask an Expert for Answer!!
JAVA Programming: Implement in different classes and that have the same
Reference No:- TGS0995730

Expected delivery within 24 Hours