Exercise 03: Default values of variables

  Quizzes     Assignments     Puzzles     Easy exercises     Required knowledge  
Easy Exercises   For Beginners
Select    01   02   03   04   05   By title 

If variables are not initialized, they have a standard value. For example, the value of uninitialized boolean variable is “false”, but a standard value of an integer variable is 0…etc.

Read More: Java Primitive Data Types

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.





Sar Maroof is a professional software development teacher, gives master classes and publishes technical articles. He is also an expert software developer and worked for several big as well as small companies and later as a freelancer.

 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 *