Please find out the output.

Asked By 0 points N/A Posted on -
qa-featured

What will happen when you compile and run the following code? public class MyClass{ static int i; public static void main(String argv[]){ System.out.println(i); } } 1) Error Variable i may not have been initialized 2) null 3) 1 4) 0

SHARE
Answered By 0 points N/A #90363

Please find out the output.

qa-featured

This is the same like your question. If you given the following code what will element?

public class MyVal{
public static void main(String argv[]){
        MyVal m = new MyVal();
        m.amethod();
 
 
public void amethod(){
boolean b[] = new boolean[5];
        }

1) 0
2) null
3) ""
4) none of these options

So the answer will be option 4) none of these options

Sneaky one here. Array element numbering starts at 0, therefore there is no element 5 for this array. If you were to attempt to perform

System.out.println(b[5])
You would get an exception.

If you attempt to access an unassigned variable an error will be generated. For example

class MyClass{

        public static void main(String argv[]){

                int p;

                int j = 10;

                j=p;

        This code will result in an error along the lines

"error variable p might not have been assigned"

class MyClass{

static int p;

        public static void main(String argv[]){

                int j = 10;

                j=p;System.out.println(j);

                }

The default value for an integer is 0, so this will print out a value of 0. The default values for numeric types is zero, a boolean is false and an object reference is the only type that defaults to a null.

Login/Register to Answer

Related Questions