Why will the code not compile?

Given:

public class Basic {
private static int letter;
public static int getLetter();
public static void Main(String[] args) {
System.out.println(getLetter());
}
}

Why will the code not compile?

Given:

public class Basic {
private static int letter;
public static int getLetter();
public static void Main(String[] args) {
System.out.println(getLetter());
}
}

Why will the code not compile?

A.
A static field cannot be private.

B.
The getLetter method has no body.

C.
There is no setLetter method.

D.
The letter field is uninitialized.

E.
It contains a method named Main instead of main

Explanation:
The getLetter() method needs a body.



Leave a Reply 5

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


sree

sree

Option E,B are correct

It should be
public static void main(String[] args).

Throws error if you use Main(String[] args)

Jazz

Jazz

B,E

Explanation:

B: The getter of letter :: getLetter(){ //has no body }
E: Error: (Main method not found in class Basic ) so it should be defined as:
public static void main(String[] args){..}

Tommy

Tommy

The correct answer is B. The getLetter() method has no body.
Answer E is not correct because the question is: why will the code not compile?

So, we don’t look what will happens during the runtime. The only thing that interest us is whether there are any errors during the compile time, before running the program. And that’s the reason why B is the only correct answer.

James

James

The Answer is B.

Answer E is not correct. The code will still successfully compile without a proper main method. The method Main is a perfectly acceptable method.

Surfo

Surfo

La respuesta es B. Por que la pregunta solo dice : por que no compila. si la pregunta fuera: por que no compila y no corre, las respuestas serian: B y E