Section 7 Quiz 1 Dan 2

Share Embed Donate


Short Description

Section 7 Quiz 1 Dan 2...

Description

Section 7 Quiz 1 - L1-L3 (Answer all questions in this section) 1.

When you write your own constructor, the default constructor is no longer available. True (*) False

2.

You create an Employee object with a String employeeName field. What is the default value for employeeName? null (*) A space “Name” “default”

3.

In Java, the this keyword can be used to reference the current object’s fields and methods. True (*) False

4.

Which statement is true about the default constructor of a class? Java automatically provides a constructor for every class. (*) You must write a default constructor. The default constructor always returns void. Default constructor should have at least one argument.

5.

How would you instantiate the Employee class from a main method located in another class? public class Employee{ private String name; private double salary; public Employee(String n, double s){ name = n; salary = s; } } Employee emp1 = new Employee(); Employee emp1 = new Employee(50000); Employee emp1 = new Employee(50000, "Syam"); Employee emp1 = new Employee("Syam", 50000); (*)

6.

An object reference with a null value points to an empty location in memory. True (*) False

7.

Objects are accessed using reference variables. True (*) False

8.

What is the output of the following code? String s1 = "Hello"; String s2 = "Welcome!"; s1 = s2; System.out.println("s1: " +s1); System.out.println("s2: " +s2); s1: Welcome! s2: Welcome! (*) s1: Hello s2: Welcome! s1: Hello s2: Hello s1: Welcome! s2: Hello

9.

Java developers don't need to know an object’s location in memory. True (*) False

10.

In this statement, identify the type of the variable s. Student s = new Student(); String Student (*) null Class

11.

Which two statements are true about objects of the same class?

(Choose all correct answers)

Each object will have the same reference variable to the location in memory. Each new instance of an object will have a different location in memory. (*) All objects of the same class have the same methods. (*) All objects are equal. 12.

In the following statements, how many employee objects are created? Employee e1 = new Employee(); Employee e2 = new Employee(); Employee e3 = new Employee(); 0 1 3 (*) 2

13.

How can you retrieve a value from a method? Pass a variable as an argument to the method. Define a variable as a field member of the method Use a return statement and define the method’s return type as non-void (*) Define the method return type as void

14.

Class name should follow Camel casing rules. True (*) False

15.

First, you decide the radius of each circle in the logo. Then using the same radius you draw 5 circles of same size. All these circles will have properties like radius and color. All circles share behaviors to calculate circumference and area. Can you identify which of the following is an object? circle (*) circumference fiveCircles radius

16.

What is the output of the following code? String s1 = "Hello"; String s2 = "Welcome!"; s1 = s2; System.out.println("s1: " +s1); System.out.println("s2: " +s2); s1: Hello s2: Welcome! s1: Hello s2: Hello s1: Welcome! s2: Welcome! (*) s1: Welcome! s2: Hello

17.

Objects are accessed using reference variables. True (*) False

18.

Objects are stored within the heap memory. True (*) False

19.

Which two statements are true about objects of the same class? (Choose all correct answers) All objects are equal. All objects of the same class have the same methods. (*) Each new instance of an object will have a different location in memory. (*) Each object will have the same reference variable to the location in memory.

20.

21.

22.

23.

24.

25.

Java automatically clears the memory once occupied by an object using garbage collection. True (*) False The structure of a class consists of properties and behaviors. True (*) False Variables created within a method can be accessed outside that method. True False (*) First, you decide the radius of each circle in the logo. Then using the same radius you draw 5 circles of same size. All these circles will have properties like radius and color. All circles share behaviors to calculate circumference and area. Can you identify which of the following is an object? circle (*) circumference fiveCircles radius In Java, the this keyword can be used to reference the current object’s fields and methods. True (*) False How could you write the Employee constructor so that its parameters are named the same as the fields they’re initializing? public class Employee{ private String name; private double salary; public Employee(String name, double salary){ //initialize name //initialize salary } } public Employee(String name, double salary){ name = name; salary = salary; } public Employee(String name, double salary){ name = this.name; salary = this.salary; } public Employee(String name, double salary){ this.name = name; this.salary = salary; } (*) public Employee(String name, double salary){ this.name = this.name; this.salary = this.salary; }

26.

A constructor is a special method which is commonly used to set the initial values of an object’s fields. True (*) False

27.

Which has a default value of null? boolean int String (*) double

28.

You create an Employee object with a String employeeName field. What is the default value for employeeName? null (*) A space “Name” “default”

29.

Which two statements are NOT true about constructors?

(Choose all correct answers)

A constructor method may return a value. (*) A constructor method is called once for each instance of an object. The constructor method is called during instantiation. A constructor method has a void return type. (*) Section 7 Quiz 2 - L4-L6 (Answer all questions in this section) 1.

Methods can call other methods in the same class. True (*) False

2.

Method overloading can be a useful technique for defining methods with similar functionality or calculations. True (*) False

3.

How would you complete this code so that one add method calls the other add method? public int add(int a, int b, int c) { return(a+b+c); } public int add(int a, int b){ //Complete this line. } return add(a, b, 0); (*) return (a, b); return add(a, b, c); return (a, b, c);

4.

Which statement is true? You must write at least one constructor in your class. A constructor can be written to accept arguments. (*) The default constructor can accept arguments. The default constructor is still available when you add your own constructor.

5.

Which two statements are true? (Choose all correct answers) An object can access another object’s main method. An object can access another object’s public methods. (*) An object can access another object’s public fields. (*) An object can access another object’s public constructor.

6.

To make fields directly accessible to other classes, the class fields must be marked public. True (*) False

7.

Which two are access modifiers? public (*) final private (*) static

8.

An object reference directs you from one object to another. True (*) False

9.

Which two statements are true about getter methods? (Choose all correct answers) You must have a setter method if you have a getter method. Getters usually accept no arguments. (*) Getters have a public access modifier. (*) Getter methods typically return void.

10.

Access and visibility of a class should be limited as much as possible. True (*) False

11.

What is encapsulation? A technique for including primitives within an ArrayList. A technique for debugging. A technique for writing more than one main method. A technique for limiting one class’s visibility to another. (*)

12.

Given the following code, why does your IDE complain that “non-static variable name cannot be referenced from a static context”? public class Employee{ public static int employeeID; public String name; public static void display(){ System.out.println(employeeID); System.out.println(name); } }

(Choose all correct answers)

Static variables cannot be referenced from methods. It would be possible to call the display() method and attempt to reference an object’s name before any object exists. (*) The variable name has a null value. Static variables are only accessible from instance methods. 13.

If you need to make a particular variable belong to a class rather than any individual instance, what type of variable should you use? A static variable. (*) A local variable. A public variable. A private variable.

14.

Static variables of a class can be accessed, even if the class has not been instantiated. True (*) False

15.

You never need to instantiate a Math object. True (*) False

16.

Which statement is true? A constructor can be written to accept arguments. (*) The default constructor is still available when you add your own constructor. The default constructor can accept arguments. You must write at least one constructor in your class.

17.

All overloaded methods share the same name. True (*) False

18.

Method overloading can be a useful technique for defining methods with similar functionality or calculations. True (*) False

19.

An object must be instantiated before its non-static fields and methods can be accessed. True (*) False

20.

The fields and methods of the Math class cannot be directly accessed as they are static. True False (*)

Correct

Correct

7. for Review (1) Points

Static variables of a class can be accessed, even if the class has not been instantiated.

Mark

True (*)

False

Correct

Correct

8. If you need to make a particular variable belong to a class rather than any individual instance, what type of variable should you use? Mark for Review (1) Points

A static variable. (*)

A local variable.

A public variable.

A private variable.

Incorrect

Incorrect. Refer to Section 7 Lesson 6.

9.

What is encapsulation? Mark for Review

(1) Points

A technique for writing more than one main method.

A technique for limiting one class’s visibility to another. (*)

A technique for including primitives within an ArrayList.

A technique for debugging.

Correct

Correct

10.

Which two statements are true about getter methods? Mark for Review

(1) Points (Choose all correct answers)

Getters usually accept no arguments. (*)

You must have a setter method if you have a getter method.

Getters have a public access modifier. (*)

Getter methods typically return void.

Incorrect Incorrect. Refer to Section 7 Lesson 5. 11. Access and visibility of a class should be limited as much as possible. (1) Points

Mark for Review

True (*)

False

Correct

Correct

12.

Which two statements are true about private access modifier?

(1) Points (Choose all correct answers)

Class fields are typically marked private. (*)

Class fields marked private are visible to any class.

Class fields are typically marked public.

Class fields marked private are most secure. (*)

Mark for Review

13.

Which two are access modifiers?

Mark for Review

(1) Points (Choose all correct answers)

final

private (*)

public (*)

static

Correct

Correct

14.

Which two statements are true?

Mark for Review

(1) Points (Choose all correct answers)

An object can access another object’s public fields. (*)

An object can access another object’s public methods. (*)

An object can access another object’s public constructor.

An object can access another object’s main method.

Incorrect

Incorrect. Refer to Section 7 Lesson 5.

15.

Which two statements are true?

Mark for Review

(1) Points (Choose all correct answers)

The purpose of a getter method is to grant other classes access to public data.

The purpose of a getter method is to return the value of a private field (*)

The purpose of a setter method is to modify a public field

The purpose of a setter method is to allow private data to be modified safely (*)

Correct

Correct

16. You never need to instantiate a Math object. (1) Points

Mark for Review

True (*)

False

17. If you need to make a particular variable belong to a class rather than any individual instance, what type of variable should you use? Mark for Review (1) Points

A private variable.

A static variable. (*)

A public variable.

A local variable.

18. Static variables of a class can be accessed, even if the class has not been instantiated. (1) Points

True (*)

False 19. Setters are void return type methods. (1) Points

Mark for Review

Mark for Review

True (*)

False 20. Which three can vary in overloaded methods? (1) Points

Mark for Review

(Choose all correct answers)

Number of parameters. (*)

Order of parameters. (*)

Method return type.

The names of parameters

Types of parameters. (*)

21. You can write more than one constructor in a class. Mark for Review (1) Points

True (*)

False

Correct

Correct

22. Method overloading can be a useful technique for defining methods with similar functionality or calculations. Mark for Review (1) Points

True (*)

False

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF