Java Final Exam Questions & Answers
1. What are the features of Java?
Java is a high-level, object-oriented, platform-independent programming language. Some of its key features include:
1. Simple: Easy to learn and use.
2. Object-Oriented: Everything is treated as objects.
3. Platform-Independent: Java bytecode runs on any platform via the JVM.
4. Secure: Offers runtime security features.
5. Robust: Strong memory management and exception handling.
6. Multithreaded: Allows simultaneous execution of two or more threads.
7. Distributed: Supports distributed computing via RMI and EJB.
Example:
public class HelloJava {
public static void main(String[] args) {
System.out.println("Java is platform-independent and robust.");
2. What is the structure of a Java program? Explain about execution of Java program.
The basic structure of a Java program consists of:
- Package declaration (optional)
- Import statements
- Class definition
- main() method
Example:
public class Example {
public static void main(String[] args) {
System.out.println("Hello, World!");
Java Final Exam Questions & Answers
Execution Steps:
1. Write the Java program and save with .java extension.
2. Compile: javac Example.java
3. Run: java Example
Compilation generates a .class file (bytecode) executed by JVM.
3. Define data type. What are the data types in Java?
Data types specify the type of data a variable can hold.
Java has two categories:
1. Primitive Data Types:
- byte, short, int, long, float, double, char, boolean
2. Non-Primitive (Reference) Data Types:
- Arrays, Classes, Interfaces, Strings
Example:
int age = 25;
float marks = 88.5f;
String name = "John";
4. Define variable. How to declare variables in Java?
A variable is a name given to a memory location to store data.
Declaration Syntax:
<data_type> <variable_name> = <value>;
Java Final Exam Questions & Answers
Example:
int number = 10;
String language = "Java";
5. Define statement. What are the different types of statements in C?
A statement is a single line of code that performs an action.
Types of Statements in C:
- Expression Statement: a = b + c;
- Compound Statement: Group of statements in braces {}
- Conditional Statement: if, if-else
- Looping Statements: for, while, do-while
- Jump Statements: break, continue, return
6. Iterative statements or looping statements
These are used to execute a block of code repeatedly.
Types in Java:
- for loop
- while loop
- do-while loop
Example:
for(int i = 1; i <= 5; i++) {
System.out.println("Count: " + i);
7. Define operators. What are the types of operators in Java?
Java Final Exam Questions & Answers
Operators are symbols that perform operations on variables and values.
Types of Operators:
- Arithmetic: +, -, *, /, %
- Relational: ==, !=, >, <, >=, <=
- Logical: &&, ||, !
- Bitwise: &, |, ^, ~
- Assignment: =, +=, -=
- Unary: +, -, ++, --
Example:
int a = 10, b = 20;
System.out.println(a + b); // Arithmetic
8. Define class and object in Java. How to declare and access class members?
Class: A blueprint for creating objects.
Object: An instance of a class.
Example:
class Car {
String color = "Red";
void start() {
System.out.println("Car is starting");
public class Main {
public static void main(String[] args) {
Car myCar = new Car();
System.out.println(myCar.color);
Java Final Exam Questions & Answers
myCar.start();
9. Define constructor and what are the types of constructors in Java?
Constructor is a special method invoked when an object is created.
Types of Constructors:
- Default Constructor: No parameters
- Parameterized Constructor: With parameters
Example:
class Student {
String name;
Student() {
name = "Default";
Student(String n) {
name = n;
10. Define method overloading. What are the advantages of it? Explain with example.
Method Overloading: Defining multiple methods with same name but different parameters.
Advantages:
- Increases readability
- Better code reusability
Java Final Exam Questions & Answers
Example:
class MathOps {
int add(int a, int b) {
return a + b;
double add(double a, double b) {
return a + b;
11. Define constructor overloading. What are the advantages of it? Explain with example.
Constructor Overloading: Having multiple constructors with different parameters.
Advantages:
- Allows different ways to initialize objects
Example:
class Book {
String title;
int pages;
Book() {
title = "Unknown";
pages = 0;
Book(String t, int p) {
title = t;
pages = p;
}
Java Final Exam Questions & Answers