Presentation is loading. Please wait.

Presentation is loading. Please wait.

Assignment 7 User Defined Classes Part 2

Similar presentations


Presentation on theme: "Assignment 7 User Defined Classes Part 2"— Presentation transcript:

1 Assignment 7 User Defined Classes Part 2

2 Question 1 Which is the wrong way to call the Class method public static long getNumStudents() in the Class student from the main method inside of student? A) int x = getNumStudents(); B) Student s = new Student(); int x = s.getNumStudents(); C) long x = Student.getNumStudents(); D) long x = this.getNumStudents();

3 Question 2 What is the problem with the following code?
public class Student{ int studentID; public static getStudent(int id) { Student s = new Student(); s.studentID = id; if(id == 0) return null; else return s; } public static void main(String args[]) for(int x = 0; x < 100; x++) Student s = getStudent(x); System.out.println(s.studentID); What is the problem with the following code? A) Incorrect Return type in getStudent() B) Invalid Assignment to Student s C) Incorrect way to call getStudent() D) s is not Initialized

4 Question 3 When can a method in a class be called without using a class or variable name before it? When a class method is being called from an instance method When a class method is being called from another class method C) When an instance method is being called from a class method When an instance method is being called from an instance method B and D A B and D

5 Question 4 Which overloaded method should be removed for the following to run in java? Method 2 or 4 Method 3 and 5 Method 2 and 3 Method 5 public class Example{ void methodA(int a){} void methodA(int a, int b){} void methodA(int b, float c){} void methodA(int b, int a){} void methodA(float c, int a, int b){} }

6 Question 5 What is the problem with the following code?
A) Incompatible variable passed to call this(id / 2) B) Can’t call a constructor after first line of another constructor C) Can’t use more than one constructor type in the class Student D) Can’t pass more than one variable to a constructor public class Student{ int studentID; String name; public Student(int id, String name) { this(id / 2); this(name); } public Student(int id) studentID = id; public Student(String name) this.name = name;


Download ppt "Assignment 7 User Defined Classes Part 2"

Similar presentations


Ads by Google