Java Program Structure tutorial and MCQ
Table of Contents
Study and learn Java Program Structure questions and answers on basic program structure and syntax. Attend job interviews easily with these MCQs.
All java programs contain some class definition. All functions or methods are kept inside the class. You need to know how to place a main method with public static void inside Class definition. Let us know the basic structure of a Java Program for better understanding next Java Tutorials in the queue.
Java Program Structure
Here is the sample Hello Java program. Check the tutorial on installing and running Java.
We shall see each line and analyse.
class HelloWorld { /* Class variables (static) can come here */ // instance variables (non static) come here public static void main(String args[]) { int a=10; float b=12.345; System.out.println("Hello World Java"); } }
Java Program Structure Observations are below.
- Keywords appearing in the above example are class, public, static, void, int and float.
- A Class is like an enclosure or a bigger block definition with inner members. A Class can contain variables and methods. It has opening and closing braces { }.
- HelloWorld is the name or identifier given to the class.
- main() is a function. In Java, you should call a function as Method. It has opening and closing braces { }.
- A multiline comment with /* and */ can contain any dummy info which is not processed.
- A single line comment with // allows us to write a single line comment.
- Every statement in Java must end with a Semicolon (;).
- Static variables and methods inside a class are called Static members or Class Members. Members can be Variables or Methods.
- Non static variables and methods inside a class are called Instance Members ( Instance Variables, Instance Methods).
- public before main method is an access modifier that exposes a method to be called from outside.
- static before main method makes it to be called even without instantiating an object.
- void before main method tells that the method does not return anything.
- Main method is taking an argument or paramter as a String Array – args[]. It allows the main method to take arguments from Command Line when running the program.
- Variables a and b are local variables that die after main method finishes execution.
- System.out has methods to read and print. Here we are using println which prints a new line after printing text.
- A Class can, in turn, contain variables of another class type similar to a Structure Type Variable in C language.
[WpProQuiz 51]
Java Program Structure MCQ
1) Every statement in Java language should end with a?
A) Dot or Period
B) Comma
C) Semicolon
D) Colon
Answer [=] C
2) A function in C language is similar to what in Java language?
A) Method
B) Member
C) Variable
D) None of the above
Answer [=] A
3) All methods and variables in Java language are kept inside a?
A) File
B) Class or Interface
C) static method
D) main
Answer [=] B
4) What is the need to mention “static” before main method?
A) To call main method without creating an object of class
B) To make main method as class method common to all instances
C) Both A and B
D) None of the above
Answer [=] C
5) In standalone Java applications, which method is mandatory?
A) main method
B) show method
C) display method
D) print method
Answer [=] A
Explanation:
main() is the starting point of execution.
6) What is the use of Access modifier “pubic” in Java language?
A) To hide the main method from misuse
B) To call the main method outside of Class or Package by JVM
C) To protect main method
D) None of the above
Answer [=] B
7) Choose a Single Line Comment in Java Language below?
A) //Some comments
B) Some comments//
C) /*Some comments*/
D) */Some comments/*
Answer [=] A
8) Choose a multiline comment in Java language below?
A)
/*comments are going cars are moving*/
B)
*/comments are going cars are moving/*
C)
//comments are going cars are moving//
D) None of the above
Answer [=] A
9)
String args[] in main method are used for? public static void main(String args[]) { // }
A) Passing arguments at compile time
B) Passing arguments at run time
C) Counting number of words
D) Nothing
Answer [=] B
Explanation:
C:/>java Hello AFRICA GOLDMINE Hello - Class name AFICA - First argument GOLDMINE - Second argument
10) What is the default return type of a method in Java language?
A) void
B) int
C) short
D) None of the above
Answer [=] D
Explanation:
You have to explicitly mention a return type.
11) Name of a Class, Variable, Method or an Interface in Java language is called?
A) Argument
B) Value
C) Identifier
D) None of the above
Answer [=] C
12) A valid identifier in Java language may contain which characters?
A) 0-9
B) A-Z, a-z
C) $, _ (Underscore)
D) All the above
Answer [=] D
13) A valid Identifier or name in Java language can start with which character?
A) a-z, A-Z
B) $, _
C) 0-9
D) A and B
Answer [=] D
Explanation:
Up to JDK 8, only a single underscore symbol can be used as an identifier of a class, method or a variable. JDK 9 does not allow.
14) What are the valid White Spaces available in Java language?
A) Space
B) Enter
C) Tab
D) All the above
Answer [=] D
15) Choose a correct version of Java Documentation Comment?
A)
/* comments */
B)
/** comments */
C)
/* comments **/
D)
/** comments **/
Answer [=] B
Explanation:
Documentation comment allows writing notes about the program. javadoc commmand on the source java file generates a HTML Java documentation with the embedded comments.