COMP SCI
What is the output of the following program? public class Main { private String str = "bar"; public void run() { Main m = new Main("foo"); System.out.println(m.getString()); } public Main(String str) { str = str; } public String getString() { return str; } }
bar **In Main(String str), the parameter name `string` shadows the instance variable name string. The statement string = string assigns the parameter string to itself, and so the assignment has no effect outside of Main(String str). Therefore, getString() will return the static variable `bar`.
public class Person { public String name; public Person(String name) { this.name = name; } public void changeName(String name) { this.name = name; } public void printName() { System.out.println(this.name); } } what will this code print? Person myPerson = new Person("Bob"); myPerson.changeName("Joe"); myPerson.name = "John"; myPerson.printName();
john
public class Athlete { public Athlete(String name) { this.name = name; } } what is missing from the class definition?
need to declare the "name" instance variable
Mark the valid way to create an instance of Foo given the following code: public class Foo { int bar; String stoo; public Foo() { this.bar = 0; this.stoo = ""; } public Foo(int bar) { this.bar = bar; stoo = "You."; } }
Foo fee; fee = new Foo();
Based on this code snippet public class Shape { public String getShapeName() { return "Shape"; } } public class Rectangle extends Shape { public String getShapeName() { return "Rectangle"; } } public class Square extends Rectangle {} public class Oval extends Shape { public String getShapeName() { return "Oval"; } } public class Circle extends Oval { public String getShapeName() { return "Circle"; } } what does this program output? Shape shape1 = new Shape(); Shape shape2 = new Rectangle(); Shape shape3 = new Square(); Shape shape4 = new Circle(); System.out.println(shape1.getShapeName()); System.out.println(shape2.getShapeName()); System.out.println(shape3.getShapeName()); System.out.println(shape4.getShapeName());
Shape Rectangle Rectangle Circle
Given the definitions of class A and class B class A { public int i; public int j; public A() { i = 1; j = 2; } } class B extends A { int a; public B() { super(); } } what is the output of this code B obj = new B(); System.out.println(obj.i + " " + obj.j);
1 2
You want to create a class called FavoriteMovies that inherits from Movies. Movies has public instance variables, private methods, and a constructor. Which of the options below will NOT be inherited from Movies in a FavoriteMovies class? I. The public instance variables in Movies. II. The Movies constructor. III. The private methods of Movies.
II and III
Given an instance of the Athlete class called athlete, what is the proper way to set the value of the jersey number after it has been instantiated? public class Athlete { private String first_name; private String last_name; private int jersey; public int getJersey() { return this.jersey; } public Athlete(String first_name, String last_name, int jersey) { this.first_name = first_name; this.last_name = last_name; this.jersey = jersey; } }
You cannot set the jersey, since jersey is private and there is no setter method.
Given an instance of the Athlete class called athlete, what is the proper way to get the value of the jersey number? public class Athlete { private String first_name; private String last_name; private int jersey; public int getJersey() { return this.jersey; } public Athlete(String first_name, String last_name, int jersey) { this.first_name = first_name; this.last_name = last_name; this.jersey = jersey; } }
athlete.getJersey()
What is the output of the following program? public class Main { private static int n = 0; public void run() { Main m1 = new Main(); Main m2 = new Main(); Main m3 = new Main(); m1.foo(); } public Main() { n = n + 1; } public void foo() { System.out.println(n); } }
3 *number of Mains
Refer to the code listing from the previous question. Suppose the following method is added: public void setN(int newValue) { n = newValue; } What would be the output of the program if run() were changed to the following: public void run() { Main m1 = new Main(); Main m2 = new Main(); Main m3 = new Main(); m1.setN(5); m3.foo(); }
5 *number in parentheses
Mark the valid way to create an instance of Athlete given the following Athlete class definition: public class Athlete { String first_name; String last_name; int jersey; public Athlete(String first_name, String last_name, int jersey) { this.first_name = first_name; this.last_name = last_name; this.jersey = jersey; } }
Athlete athlete = new Athlete("Dirk", "Nowitzki", 41);
Given this code snippet, public class Person { public String name; public Person(String name) { System.out.println(name); this.name = name; } public void printName() { System.out.println(this.name); } } public class Miner extends Person { public Miner(String name) { super(name); } } what will this program print? Person somePerson = new Person("Bob"); Miner mikeTheMiner = new Miner("Mike"); mikeTheMiner.printName();
Bob Mike Mike
Given the following definition for the class Athlete: public class Athlete { String first_name; String last_name; int jersey; public Athlete(String first_name, String last_name) { this.first_name = first_name; this.last_name = last_name; this.jersey = 0; } public Athlete(String first_name, String last_name, int jersey) { this.first_name = first_name; this.last_name = last_name; this.jersey = jersey; } } Which of the following are valid instantiations of the class Athlete? I - Athlete joe = new Athlete("Joe", "Montana"); II - Athlete joe = new Athlete("Joe, "Montana", "16"); III - Athlete joe = new Athlete("Joe", "Montana", 16);
I & III
Given the class definitions of Vector2D and Vector3D below: public class Vector2D { public int x; public int y; public Vector2D() {} public Vector2D(int x,int y) { this.x = x; this.y = y; } // other code } public class Vector3D extends Vector2D { public int z; // other code } Which of the constructors that follow would be valid to put in the Vector3D class? Possible constructors for Vector3D: I. public Vector3D() {} II. public Vector3D(int x, int y, int z) { super(x,y); this.z = z; } III. public Vector3D(int x, int y) { this.x = x; this.y = y; this.z = 0; }
I, II, III
The Java Classes Skeleton, Spider, and Zombie all extend the Java Class Monster. The Monster Class is defined below. public class Monster { public String name; public String type; private int x; private int y; public Monster(String name, String type) { this.name = name; this.type = type; this.x = 0; this.y = 0; } public void move(x, y) { this.x = x; this.y = y; } } What variables and methods do the Skeleton, Spider, and Zombie Classes inherit?
The variables name and type and the method move
Which methods of class Foo can be called without an actual instance of the class Foo? public class Foo { public static void foo() { ... }; public void bar() { ... }; public void baz() { ... }; }
foo()
public class Pokemon { private String name; private int health; public Pokemon(String name, int health) { name = name; health = health; } } what is wrong with the class definition?
needs "this." ex: this.name = name;
Given this Athlete class, which of the following setter methods for the jersey variable is correct? public class Athlete { private String first_name; private String last_name; private int jersey; public int getJersey() { return this.jersey; } public Athlete(String first_name, String last_name, int jersey) { this.first_name = first_name; this.last_name = last_name; this.jersey = jersey; } }
public void setJersey(int jersey) { this.jersey = jersey; }