Test Series - java

Test Number 57/64

Q: Can you pass a sentence with multiple words separated by spaces as a single command-line argument in Java?
A. Yes
B. No
C. 
D. 
Solution: You should keep the string or sentence within double quotes to tell the JVM that you are passing a single command-line argument.
Q: What is the output of the below Java program with command-line arguments?
public class CommandLineArgs1
{
  public static void main(String[] args)
  {
    for(String str: args)
    {
      System.out.println(str);
    }
    if(args.length == 0)
      System.out.println("No arguments passed");
}

c:folder>java CommandLineArgs1 car brake horn
A. car brake horn
B. car brake horn
C. horn brake car
D. horn brake car
Solution: Each space tells the JVM to accept a new argument. So, in the above example, we have passed 3 arguments.
Q: Is there any limit to the number of spaces between two arguments of command-line arguments in Java?
A. Yes
B. No
C. 
D. 
Solution: You can keep any number of spaces between two adjacent arguments.
Q: Choose the correct way of receiving command-line arguments with in the MAIN method in Java?
A. public static void main(String[] args) { }
B. public static void main(String args[]) { }
C. public static void main(String anyName[]) { }
D. All the above.
Solution: You can give any name to the String-array variable accepting command-line arguments.
Q: what is the output of the below Java program with command-line arguments?
public class CommandLineArguments2
{
  public static void main(String[] args)
  {
    System.out.println(args[1]);
  }
}

C:folder>java CommandLineArguments2 TIGER
A. CommandLineArguments2
B. TIGER
C. Compiler error
D. None
Solution: Program name or class-name is not considered as a command-line argument. In the above example, one argument is passed (TIGER) which can be accessed by args[0].
Q: Which is the exception or error that is thrown if a non-existing command-line argument is referred to in a Java program?
A. StackOverflowError
B. IndexOutOfBoundsException
C. ArrayIndexOutOfBoundsException
D. ArithmeticException
Solution: As the command-line arguments are processed as a String-array, the exception will be ArrayIndexOutOfBoundException only.
Q: Any type of data that can be typed on a console or ECLIPSE can be passed as a command-line argument. State TRUE or FALSE.
A. TRUE
B. FALSE
C. 
D. 
Solution: TRUE
Q: Which are the methods used to parse string values to the respective data types in Java?
A. Boolean.parseBoolean(), Byte.parseByte()
B. Integer.parseInt(), Long.parseLong()
C. Float.parseFloat(), Double.parseDouble()
D. All the above
Solution: All the above are static methods used to parse String values to the respective types. Parsing is nothing but converting.
Q: What is the output of the below Java program?
public class CommandLineArguments3
{
  public static void main(String[] args)
  {
    String name = args[0];
    int age = Integer.parseInt(args[1]);
    Boolean married = Boolean.parseBoolean(args[2]);
    Float salary = Float.parseFloat(args[3]);
    System.out.println("Name="+name + ", Age=" + age + ", Married=" + married + ", Salary=$"+ salary);
  }
}

C:folder>java CommandLineArguments3 Marker 25 false 5025.35
A. Name=Marker, Age=25, Married=false, Salary=$5025.35
B. Name=Marker, Age=25, Married=false, Salary=$5025
C. Compiler error
D. None
Solution: The order of parsing is very important. In the above program, we carefully chose the argument index and parsed correctly.

You Have Score    /9