Lompat ke konten Lompat ke sidebar Lompat ke footer

Section 7 Quiz 1 - L1-L3

 


1. Which is stored within the stack memory?

Instance variables

Local variables (*)

Objects

Strings

Correct

(1/1) Point

2. 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: Hello

s1: Hello

s2: Welcome!

s1: Welcome!

s2: Hello

s1: Welcome!

s2: Welcome! (*)

Correct

(1/1) Point

3. Objects are stored within the heap memory.

benar (*)

Palsu

Correct

(1/1) Point

4. Java automatically clears the memory once occupied by an object using garbage collection.

benar (*)

Palsu

Correct

(1/1) Point

5. Which type of memory is allocated for the code below?


int x = 1;

int y = 2;

x=y;

No memory is allocated

PileDriver memory

Stack memory (*)

Heap memory

Correct

6. Objects are accessed using reference variables.

benar (*)

Palsu

Correct

(1/1) Point

7. Which two statements are true about the main method?

(Pilih semua jawaban yang benar)

The main method is commonly used to instantiate objects. (*)

The main method should be able to freely manipulate an object’s fields.

The main method should store the properties and behaviors of objects.

The main method should be as simple as possible. (*)

Correct

(1/1) Point

8. The structure of a class consists of properties and behaviors.

benar (*)

Palsu

Correct

(1/1) Point

9. You have created an Employee class with all required fields and methods. 10 employees join the company. Should you copy and paste the Employee class for all 10 employees?

benar

Palsu (*)

Correct

(1/1) Point

10. Which two statements are NOT true about constructors?

(Pilih semua jawaban yang benar)

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. (*)

Correct

11. 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;

}

Correct

(1/1) Point

12. 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); (*)

Correct

(1/1) Point

13. What will happen when you try to access an object reference with a null value?

NullPointerException. (*)

The value null is retrieved from the memory location.

An empty object is returned.

You will get a compilation error.

Incorrect. Refer to Section 7 Lesson 3.

(0/1) Point

14. An object reference with a null value points to an empty location in memory.

benar (*)

Palsu

Correct

(1/1) Point

15. In Java, the this keyword can be used to reference the current object’s fields and methods.

benar (*)

Palsu

Correct

Posting Komentar untuk "Section 7 Quiz 1 - L1-L3"