Exercise 03: Default values of variables

Very Easy Exercises For Beginners
Very Easy Exercises   For Beginners  E = Exercise
 E01   E02   E03   By title 

Java exercise 03: Default values of variables

Level: Very easy

What is written to the standard output as the result of executing the following code?

public class DefaultValue
{
  int height;
  int width = 6;
  double wage;
  boolean isBig;
  String text;
  
  public static void main(String[] args)
  {
    DefaultValue df = new DefaultValue();
    System.out.print(df.height + ", ");
    System.out.print(df.width + ", ");
    System.out.print(df.wage + ", ");
    System.out.print(df.isBig + ", ");
    System.out.print(df.text);
  }
}
Author: Sar Maroof

Answer explanation

  1. The variables height, width, wage, isBig and the object text are not initialized.
  2. If variables are not initialized the value of int = 0, double = 0.0, boolean = false and object = null.

The correct answer is: 0, 6, 0.0, false, null.

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