Java quiz 38: Using the keyword “this”

<< Java Quizzes   Green = Easy,  Blue = Normal,  Red = Hard
 Q25   Q26   Q27   Q28   Q29   Q30   Q31   Q32   Q33   Q34   Q35   Q36   Q37   Q38   Q39 
 By title 
>>

This quiz shows how you can initialize instance variables using constructors. It clearifies
also the usage of the keyword “this” to call a constructor from another one.

Java quiz 38: Using the keyword “this”

Level: Normal

What is the output of the following code?

 
public class Student
{
  String name;
  char gender = 'm'; // m male, f female
  
  Student()
  {
    this("Hannah", 'f');
  }
  Student(String name, char gender)
  {
    this.gender = 'm';
  }
  public static void main(String[] args)
  {
    Student st = new Student();
    Student st2 = new Student("Emily", 'f');
    st2 = st;
    System.out.print(st2.name + ", " +
                     st2.gender);
  }
}
Author: Sar Maroof

Select the correct answer.

a. This program writes “Hannah, m” to the standard output.
b. This program writes “Emily, f” to the standard output.
c. This program writes “Hannah, f” to the standard output.
d. This program writes “null, m” to the standard output.
e. This program writes “null, f” to the standard output.
f. Illegal at compile time.
g. Illegal at run time.

Answer explanation

  1. The statement Student st = new Student(); creates the object st.
  2. By calling the no-argument constructor the statement “this(“Hannah”, ‘f’);” is executed, which calls the two-argument constructor of the class Student.
  3. By calling the two-argument constructor the statement “this.gender = ‘m’;” is executed which assings the value ‘m’ to the gender.
  4. In the two argument constructor the value of the argument name is not assigned to the instance variable name. Therfore the initial value of the instance variable name remains, which is null.
  5. The statement st2 = st; means that the st2 refers to st.

The correct answer is: d.

 

If you found this difficult, you might consider reading my book Java quizmaster for beginners. It is easy to understand, organized to learn Java in 17 days and it guides you to master Java code by solving 105 quizzes and 117 assignments. This book explains each chapter properly before starting with exercises and assignments. It is available on Amazon. See inside the book here!

Please, leave your questions, feedback and suggestions in the comments below!
SarMaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.




Sar Maroof is graduated from HBO Amsterdam "higher professional education". He is a professional computer programming teacher, gives master classes and publishes technical articles. His experience with developing software started from 2001 and he has worked as a software developer for several big as well as small companies and later as a freelancer. He is the author of Java quizmaster and Build a Java application in 7 days.

Books by Sar Maroof

           

 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!

Leave a Reply

Your email address will not be published. Required fields are marked *

Sarmaroof © 2017