W1: OOP
Please submit your answers to the questions as comments in a W1.txt
file you’ll be writing in this lecture.
Grading rubric and submission
When you are done, submit your W1.txt
file to BB.
You will be graded on the following:
Item | Points |
---|---|
Answers are correct | 100 |
Questions
1
Which of the following is a reference type and which is a primitive type?
int a;
double b;
int c[] = {1, 2, 3};
String s = "Hello World";
2
Four part question:
What is a static method in Java?
Why does the main method need to be a static method?
public class Hello {
public static void main(String[] args) {
System.out.println("hello, world");
}
}
What is a static field in java?
What is the relationship between static fields and static methods, versus non-static fields and non-static methods?
3
What is the output of the following programs?
/* Program 1 */
public static void main(final String args[]) {
String choice = new String("A");
if (choice == "A") {
System.out.println("Correct");
}
else {
System.out.println("Wrong");
}
}
/* Program 2 */
public static void main(final String args[]) {
String choice = new String("A");
if (choice.equals("A")) {
System.out.println("Correct");
}
else {
System.out.println("Wrong");
}
}
4
Does the below program change the season? Why, or why not?
static void change_season(String str) {
= "Spring";
str }
public static void main(final String args[]) {
String season = "Winter";
change_season(season);
System.out.println("The current season is: " + season);
}
5
What is the this
keyword in Java?
6
What is the output of the main method below? Please explain.
public class Point {
double x = 0;
double y = 0;
public Point(double x, double y) {
= x;
x = y;
y }
}
public static void main(final String args[]) {
Point point = new Point(1, 2);
System.out.println("X: " + point.x + " Y: " + point.y);
}
7
In the Point
class below, how does Java choose between the two constructors?
public class Point {
private double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public Point(Point other) {
this.x = other.getX();
this.y = other.getY();
}
}