Core Java QNA | Software testing interview questions | Core Java interview questions | Tech Bharat
1) If there is no main method, what will happen?
Ans. If the main method is not present, then at compile time there is no error but we will get a run-time error
2) Does the main method return any value?
Ans. The main method has a return type of void means it does not return anything
3) What is the main method?
Ans. The main method is the entry point of any Java program. Without the main method, we can not run any Java program. It is the starting point of any Java program.
Syntax:
public static void main(String[] args)
4) (String[] args) What is this in the main method?
Ans. String[] args is a parameter that receives command line arguments. Each value passed from the command line is stored as a string in the array
5) main(String[] args) in this insted of args can I write something else like java, or Selenium like this (String[] Selenium)?
Ans. Yes, variables can be anything. Only the type and structure matter. Type should be string and structure should be String[] variable_name
6) public static void main(string args[]) { } is this the right main method? Will this work? ([] position is changed)
Ans. We will get a compile-time error because string is a class and class names should be start with capital letters. The position of [] does not matter; this will work fine if first letter of the string is capitalized, irrespective of the position of [].
7) public static void main(string[]args) { }, is this right main method, is this will work? (i have removed the space between []args)
Ans. We will get a compile-time error because the string is a class and the class name should start with a capital letter. The position and space between [] and the variable, string class is not matter, this will work fine if first letter of the string is capitalized, irrespective of the position of [] space
8) public static void main(String...args) { }, is this the right main method, is this will work? ( i have removed [] and added ...)
Ans. Yes, this is valid. This is called var args (variable-length arguments).
Java treats it similarly to an array.
It will still work as the main method:
public static void main(String... args)
9) static public void main(String[] args) { is this valid main method ?
Ans. This is valid as the order of public and static does not matter
10) public void static main(String[] args) {} is this valid main method?
Ans. This is invalid because the correct order must be either:
public static void or
static public void
11) public static main void (String[] args) {} is this valid main method?
Ans. No, this is invalid syntax.
The order is incorrect — void must come before the method name main.
12) Who Gets Highest Priority in Execution?
Ans. The main method has the highest priority in the execution as this is the starting point of execution.
Post a Comment