Menu
Topics Index
...
`

Classes and Objects2759


If you need explanation Read this topic

If you need Answer Take test on this topic

Classes and Objects2759
What will be the output of following program?
public class ClassInitialization {

    boolean b;
    byte by;
    char A = 65;

    public static void main(String[] args) {
        ClassInitialization demo = new ClassInitialization();
        System.out.println("b = " + demo.b);
        System.out.println("by = " + demo.by);
        System.out.println("c = " + demo.A);
    }
}

A. b = true
by = 0
c = 65

B. b = false
by = 0
c = A

C. Some other output
D. Compilation Error or Runtime Error
Topic: Member Variable In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is B...here we have 3 instance variables boolean,char,byte..

here we didnt initilize the b and by so that for every boolean value default value is false and here byte is an type integer so that default value is 0 and char we are giving 65 which as ascii value for the 'A'

Posted by Uday Kumar    2014-11-17 12:23:38


ans b bcz
first in main function demo object is created of ClasssInitialization type
which has 3 variables of which A of char type is initialized to 65 i.e ASCII A
In the first print statement demo.b is printed since it is not initialized it prints boolean default value "false"
In the second print statement demo.by is printed since it is not initialized it prints byte default value "0"
The third print statement prints demo.A which is 'A' so ans is b

Posted by Pooja Singh    2014-11-17 12:25:23


ans b.In Java, when variable is declared as field (static or instance variable inside class), then initialization of that variable is optional. In other words, while declaring field variable you may or may not initialize to its value.if you are not assign value, then Java runtime assigns default value to it. and when you try to access the variable you get the default value of that variable.                                  so in this program boolean b assigned with default value false ,byte by assigned with 0.                  char a assigned with 65 which is equals to 'A'.                      65 is ascii vaue of A                                                                                      But, this is not the case with local variables or block variables.

When you declare any local/block variable, they didn’t get the default values. They must assigned some value before accessing it other wise compiler will throw an error.                                                                                                               in main program classInitialization objct is created, by using that referencr sop prints b=false,by=0,a=A

Posted by Maheshwari Natarajan    2014-11-17 17:44:01


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Pooja Singh, Maheshwari Natarajan,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Bolo Raven. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-11-18 01:27:51


© meritcampus 2019

All Rights Reserved.

Open In App