Java Quiz 7 - Classes & Functions

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

1. ________ represents an entity in the real world that can be distinctly identified. A) An object B) A data field C) A class D) A method

ANS: A

16. Consider the Code Example 2.What will be the output of Line2? a. New 1 b. New 2 c. New 3 d. None of these

ANS: B

19. Consider the Code Example 2.What will be the output of Line5? a. Seema,111-11-1111,15.0,some junk value b. compiler error because hoursWorked is not initialized c. Seema,111-11-1111,15.0,0 d. Seema,111-11-1111,0.0,0 e. None of these

ANS : C

20. Consider the Code Example 2.What will be the output of Line6?

Yaji, 222-22-2222, 20.0, 0

18. Consider the Code Example 2.What will be the output of Line4? a. Not Provided,Not Provided,10.0 ,0 c. ,None, 15.0,0 b. Not Provided,None,10.0 ,0 d. None of these

ANS : A

10. What happens when this statement is executed? Automobile car = new Automobile(1); a. An object of the Automobile class is created. b. An object of the Automobile class is declared, but not created. c. A constructor that accepts a string in the Automobile class is called. d. The default constructor in the Automobile class is called.

ANS: A

12. If the constructor that follows is in the Employee class, what other form of constructor, if any, is implicitly provided? public Employee(String name, int salary) {...} a. None b. Employee() {...} c. Employee(String name) {...}

ANS: A

17. Consider the Code Example 2.What will be the output of Line3? a. New 1 b. New 2 c. New 3 d. None of these

ANS: A

6. Which of the following statements are true? (Choose all that apply.) A) Constructors are invoked using the new operator when an object is created. B) Constructors must have the same name as the class itself. C) At least one constructor must always be defined explicitly. D) A default no-arg constructor is provided automatically if no constructors are explicitly declared in the class. E) Constructors do not have a return type, not even void.

ANS: A, B, D, E

11. If there is no constructor defined by the programmer in the Employee class, what form of constructor, if any, is implicitly provided? a. None b. Employee() {...} c. Employee(String name) {...}

ANS: B

3. An object is an instance of a ________. A) data B) class C) program D) method

ANS: B

9. A constructor a. always accepts two arguments b. has return type of void c. has the same name as the class d. always has an access specifier of private

ANS: C

7. Given the declaration Circle x = new Circle(), which of the following statement is most accurate? A) x contains an int value. B) You can assign an int value to x. C) x contains an object of the Circle type. D) x contains a reference to a Circle object.

ANS: D

2. ________ is a construct that defines objects of the same type. A) A class B) A method C) An object D) A data field

ANS: A

5. ________ is invoked to create an object. A) A constructor B) A method with the void return type C) A method with a return type D) The main method

ANS: A

4. The keyword ________ is required to declare a class. A) private B) class C) public D) All of the above.

ANS: B

13. The asterisked line of code gives a compiler error. Why? public class Test { private int x; public Test(String a) { System.out.println("Test object created"); } } public class Main { public static void main(String[] args) { **** Test t = new Test(); Test t = new Test("abc"); } }

ANS: Because as soon as the construct Test(String a) {...} was added to the Test class, the argument-less constructor was lost and hence the underlined line of code gives a compiler error.

14. The underlined code gives a compiler error. Why? public class Main { public static void main(String[] args) { A a = new A(); ******System.out.println(getX()); } } public class A { private int x; public int getX() { return x; } }

ANS: Because no object reference is provided when calling the instance method getX(). The underlined line of code should be changed to System.out.println(a.getX());

15. Consider the Code Example 2.What will be the output of Line1? a. New 1 b. New 2 c. New 3 d. None of these

ANS: C

8. The default value for data field of a boolean type, numeric type, object type is ________, respectively. A) true, 1, null B) false, 1, null C) false, 0, null D) true, 1, Null E) true, 0, null

ANS: C

Code Example 2: public class HourlyEmployee { private String name = "None"; private String ssn; private double hourlyRate = 15.0; private int hoursWorked; public HourlyEmployee(String n, String s, double hRate) { System.out.println("New 1"); this.name = n; this.ssn = s; this.hourlyRate = hRate; } public HourlyEmployee(String n, String s) { System.out.println("New 2"); this.name = n; this.ssn = s; } public HourlyEmployee() { System.out.println("New 3"); this.name = "Not Provided"; this.ssn = "Not Provided"; this.hourlyRate = 10.0; } public String GetEmployeeInfo() { return name + "," + ssn + "," + hourlyRate + "," + hoursWorked; } }

public class Program { public static void main(String[] args) { HourlyEmployee obj1 = new HourlyEmployee(); //Line1 HourlyEmployee obj2 = new HourlyEmployee("Seema", "111-11-1111"); //Line 2 HourlyEmployee obj3 = new HourlyEmployee("Yaji", "222-22-2222", 20); //Line 3 System.out.println(obj1.GetEmployeeInfo()); //Line 4 System.out.println(obj2.GetEmployeeInfo()); //Line 5 System.out.println(obj3.GetEmployeeInfo()); //Line 6 } }


Kaugnay na mga set ng pag-aaral

Industrial Economics questions- Everything

View Set

KIV/ZI 1. ročník FEK, Teorie ke zkoušce ze ZI

View Set

ECO - Ch.18 - Open-Economy Macroeconomics: Basic Concepts

View Set

Political Ideologies "Nationalism" Chapter 6

View Set

Ch 7 Intrest Groups and Political Parties

View Set

Chapter 7: Requirements -> Behavior Driven Design

View Set