Skip to content
uloscastudy.com logo

Java Fundamentals (D286)

Master Java Fundamentals (D286) with Ulosca! Struggling with Java Fundamentals (D286)? Ace your exams with ULOSCA—your ultimate study partner! 200+ Exam Practice Questions and answers – Designed to mirror real exam scenarios. Detailed Explanations &

Monthly course access through your account.

  • Course-specific practice questions
  • Detailed answer explanations
Table of Contents

Topics in this course

  • Java Fundamentals (D286) 1 practice exam 98 questions

Questions from all course topics are combined in this practice exam and have not yet been grouped into separate topics.

Java Fundamentals (D286) Study Notes

Table of Contents

  1. Introduction to Java

    • What is Java?
    • Features of Java
    • Java Development Kit (JDK) and Java Runtime Environment (JRE)
    • Writing Your First Java Program
  2. Java Syntax and Data Types
    • Variables and Data Types
    • Operators
    • Control Flow Statements (if-else, loops, switch)
  3. Object-Oriented Programming (OOP) in Java
    • Classes and Objects
    • Inheritance, Polymorphism, Encapsulation, and Abstraction
    • Constructors and Methods
  4. Arrays and Strings
    • Working with Arrays
    • String Manipulation
  5. Exception Handling
    • Types of Exceptions
    • Try-Catch Blocks
    • Custom Exceptions
  6. File Handling in Java
    • Reading and Writing Files
    • File Class Methods
  7. Case Study 1: Building a Student Management System
    • Problem Statement
    • Solution Design
    • Code Implementation
    • Analysis
  8. Case Study 2: Creating a Banking Application
    • Problem Statement
    • Solution Design
    • Code Implementation
    • Analysis
  9. Practice Questions and Answers
    • 100 Q&A with Rationales

 

1. Introduction to Java

What is Java?

Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle). It is platform-independent, meaning Java programs can run on any device with a Java Virtual Machine (JVM).

Features of Java
  • Platform Independence: Write once, run anywhere (WORA).
  • Object-Oriented: Everything in Java is an object.
  • Secure: Built-in security features like bytecode verification.
  • Multithreading: Supports concurrent execution of multiple threads.
JDK and JRE
  • JDK: Used to develop Java applications. Includes tools like javac (compiler) and java (interpreter).
  • JRE: Provides the runtime environment to execute Java programs.
Writing Your First Java Program

java

Copy

public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Hello, World!");

    }

}

 

  • Explanation:
    • public class HelloWorld: Defines a class named HelloWorld.
    • public static void main(String[] args): The main method where execution begins.
    • System.out.println("Hello, World!");: Prints "Hello, World!" to the console.

Introduction to Java programming.

 

2. Java Syntax and Data Types

Variables and Data Types
  • Primitive Data Types: int, double, char, boolean, etc.
  • Reference Data Types: Objects, arrays, and strings.

Example:

java

Copy

int age = 25;

double salary = 50000.50;

char grade = 'A';

boolean isJavaFun = true;

 
Operators
  • Arithmetic Operators: +, -, *, /, %
  • Comparison Operators: ==, !=, >, <
  • Logical Operators: &&, ||, !

 

Example:

java

Copy

int a = 10, b = 20;

if (a > b) {

    System.out.println("a is greater");

} else {

    System.out.println("b is greater");

}

 

 
Control Flow Statements
  • If-Else:

java

Copy

int marks = 85;

if (marks >= 90) {

    System.out.println("Grade A");

} else if (marks >= 80) {

    System.out.println("Grade B");

} else {

    System.out.println("Grade C");

}

 

  • Loops:

java

Copy

for (int i = 1; i <= 5; i++) {

    System.out.println("Iteration: " + i);

}


Object-Oriented Programming (OOP) in Java

Classes and Objects
  • Class: A blueprint for creating objects.
  • Object: An instance of a class.

Example:

java

Copy

class Car {

    String color;

    int speed;

 

    void accelerate() {

        speed += 10;

    }

}

 

Car myCar = new Car();

myCar.color = "Red";

myCar.accelerate();

 

Inheritance
  • Allows a class to inherit properties and methods from another class.

Example:

java

Copy

class Vehicle {

    void run() {

        System.out.println("Vehicle is running");

    }

}

 

class Bike extends Vehicle {

    void accelerate() {

        System.out.println("Bike is accelerating");

    }

}

 

Polymorphism
  • The ability of a method to perform different tasks.

Example:

java

Copy

class Animal {

    void sound() {

        System.out.println("Animal makes a sound");

    }

}

 

class Dog extends Animal {

    void sound() {

        System.out.println("Dog barks");

    }

}

 

Java Power Tools: Model software for teaching object-oriented design.

 

4. Arrays and Strings

Working with Arrays
  • Arrays store multiple values of the same type.

Example:

java

Copy

int[] numbers = {1, 2, 3, 4, 5};

for (int num : numbers) {

    System.out.println(num);

}

 

 

String Manipulation
  • Strings are immutable in Java.

Example:

java

Copy

String name = "Java";

System.out.println(name.length()); // Output: 4

System.out.println(name.toUpperCase()); // Output: JAVA

Strings and Arrays.

 

5. Exception Handling

Types of Exceptions
  • Checked Exceptions: Compile-time exceptions (e.g., IOException).
  • Unchecked Exceptions: Runtime exceptions (e.g., NullPointerException).
Try-Catch Blocks

java

Copy

try {

    int result = 10 / 0;

} catch (ArithmeticException e) {

    System.out.println("Cannot divide by zero");

}

 

Exception handling: A field study in java and. net.

 

6. File Handling in Java

Reading and Writing Files

java

Copy

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

 

public class FileExample {

    public static void main(String[] args) {

        try {

            FileWriter writer = new FileWriter("example.txt");

            writer.write("Hello, Java!");

            writer.close();

        } catch (IOException e) {

            System.out.println("An error occurred.");

        }

    }

 

7. Case Study 1: Building a Student Management System

Problem Statement

Create a program to manage student records, including adding, updating, and displaying student details.

Solution Design
  • Use classes to represent students.
  • Use arrays or collections to store student data.
Code Implementation

java

Copy

class Student {

    String name;

    int rollNo;

    double marks;

 

    Student(String name, int rollNo, double marks) {

        this.name = name;

        this.rollNo = rollNo;

        this.marks = marks;

    }

 

    void display() {

        System.out.println("Name: " + name + ", Roll No: " + rollNo + ", Marks: " + marks);

    }

}

 

public class StudentManagement {

    public static void main(String[] args) {

        Student[] students = new Student[3];

        students[0] = new Student("Alice", 1, 95.5);

        students[1] = new Student("Bob", 2, 88.0);

        students[2] = new Student("Charlie", 3, 91.0);

 

        for (Student s : students) {

            s.display();

        }

    }

}

 

Analysis
  • The program uses OOP principles to manage student data.
  • Arrays store multiple student objects, and a loop displays their details.

 

8. Case Study 2: Creating a Banking Application

Problem Statement

Develop a simple banking application to deposit, withdraw, and check balance.

Solution Design
  • Use a class to represent a bank account.
  • Implement methods for deposit, withdrawal, and balance inquiry.
Code Implementation

java

Copy

class BankAccount {

    double balance;

 

    BankAccount(double initialBalance) {

        this.balance = initialBalance;

    }

 

    void deposit(double amount) {

        balance += amount;

        System.out.println("Deposited: " + amount);

    }

 

    void withdraw(double amount) {

        if (amount > balance) {

            System.out.println("Insufficient balance");

        } else {

            balance -= amount;

            System.out.println("Withdrawn: " + amount);

        }

    }

 

    void checkBalance() {

        System.out.println("Balance: " + balance);

    }

}

 

public class BankingApp {

    public static void main(String[] args) {

        BankAccount account = new BankAccount(1000);

        account.deposit(500);

        account.withdraw(200);

        account.checkBalance();

    }

}

 

Analysis
  • The program demonstrates encapsulation by keeping the balance private and providing methods to interact with it.
  • It handles edge cases like insufficient balance during withdrawals.


 

Q&A Section

 

Question 1:

 
What is the output of the following Java code?

java

Copy

public class Main {

    public static void main(String[] args) {

        int x = 5;

        int y = x++;

        System.out.println(y);

    }

}

 

Options:
A) 5
B) 6
C) 0
D) Compilation Error

 

Correct Answer:
 
A) 5

Explanation of Correct Answer:

  • The x++ operator is a post-increment operator. This means the value of x is first assigned to y, and then x is incremented by 1.
  • In this code:
    • x is initially 5.
    • y = x++ assigns the current value of x (which is 5) to y.
    • After the assignment, x is incremented to 6, but y remains 5.
  • Therefore, the output of System.out.println(y) is 5.

 

Why Other Options Are Wrong:

B) 6: This would be correct if the code used the pre-increment operator (++x). However, x++ increments x after the assignment, so y is not affected by the increment.

C) 0: There is no logical or syntactical reason for y to be 0. This option is incorrect.

D) Compilation Error: The code is syntactically correct and will compile without errors. This option is incorrect.

 

Question 2:

 
Which of the following is NOT a feature of Java?

Options:
A) Platform Independence
B) Object-Oriented
C) Pointers
D) Multithreading

 

Correct Answer: C) Pointers

Explanation of Correct Answer:

  • Java does not support pointers explicitly. Pointers are a feature of languages like C and C++, which allow direct memory manipulation. Java avoids pointers to ensure security and simplicity.
  • Instead of pointers, Java uses references to interact with objects, which are safer and managed by the JVM.

 

Why Other Options Are Wrong:

A) Platform Independence: This is a core feature of Java. Java programs are compiled into bytecode, which can run on any device with a JVM, making it platform-independent.

B) Object-Oriented: Java is fundamentally object-oriented, meaning everything in Java is an object (except primitive data types).

D) Multithreading: Java supports multithreading, allowing concurrent execution of multiple threads within a program.

 

Question 3:

 
What is the purpose of the static keyword in Java?

Options:
A) To create a constant variable
B) To allow a method or variable to belong to the class rather than an instance
C) To prevent a class from being instantiated
D) To make a variable thread-safe

 

Correct Answer:
 
B) To allow a method or variable to belong to the class rather than an instance

Explanation of Correct Answer:

  • The static keyword is used to define class-level methods and variables. These belong to the class itself rather than any specific instance of the class.
  • For example:
  • java
  • Copy

class Counter {

    static int count = 0; // Class-level variable

    Counter() {

        count++;

    }

  • }

Here, count is shared across all instances of the Counter class.

 

Why Other Options Are Wrong:

A) To create a constant variable: Constants are created using the final keyword, not static. For example: final int MAX_VALUE = 100;.

C) To prevent a class from being instantiated: This is achieved by making the class abstract or defining a private constructor, not by using static.

D) To make a variable thread-safe: The static keyword does not inherently make a variable thread-safe. Thread safety is achieved using synchronization or other concurrency mechanisms.

 

Question 4:

 
What will be the output of the following code?

java

Copy

public class Main {

    public static void main(String[] args) {

        String str1 = "Java";

        String str2 = new String("Java");

        System.out.println(str1 == str2);

    }

}

 

 

Options:
A) true
B) false
C) Compilation Error
D) Runtime Error

 

Correct Answer:
 
B) false

Explanation of Correct Answer:

  • In Java, the == operator checks for reference equality, not content equality.
  • str1 is a string literal stored in the string pool, while str2 is a new object created in the heap memory.
  • Even though both strings contain the same content ("Java"), they refer to different memory locations. Therefore, str1 == str2 evaluates to false.

 

Why Other Options Are Wrong:

A) true: This would be correct if the code used the .equals() method, which checks for content equality. For example: str1.equals(str2) would return true.

C) Compilation Error: The code is syntactically correct and will compile without errors.

D) Runtime Error: There is no runtime error in this code. It will execute and produce the output false.

 

Question 5:

 
Which of the following is true about Java exceptions?

Options:
A) Checked exceptions are checked at runtime.
B) Unchecked exceptions are checked at compile time.
C) Checked exceptions must be handled using try-catch or declared in the method signature.
D) Unchecked exceptions are always fatal and cannot be handled.

 

Correct Answer:
 
C) Checked exceptions must be handled using try-catch or declared in the method signature

Explanation of Correct Answer:

  • Checked exceptions are exceptions that are checked at compile time. Examples include IOException and SQLException.
  • Java requires that checked exceptions either be:
    • Caught using a try-catch block, or
    • Declared in the method signature using the throws keyword.

 

Why Other Options Are Wrong:

A) Checked exceptions are checked at runtime: This is incorrect. Checked exceptions are checked at compile time, not runtime.

B) Unchecked exceptions are checked at compile time: This is incorrect. Unchecked exceptions (e.g., NullPointerException, ArithmeticException) are not checked at compile time. They occur at runtime.

D) Unchecked exceptions are always fatal and cannot be handled: This is incorrect. Unchecked exceptions can be handled using try-catch blocks, but they are not required to be handled.

 

Question 6:

 
What is the output of the following code?

java

Copy

public class Main {

    public static void main(String[] args) {

        int[] numbers = {1, 2, 3, 4, 5};

        System.out.println(numbers[5]);

    }

}

 

 

Options:
A) 5
B) 0
C) ArrayIndexOutOfBoundsException
D) Compilation Error

 

Correct Answer:
 
C) ArrayIndexOutOfBoundsException

Explanation of Correct Answer:

  • Arrays in Java are zero-indexed, meaning the first element is at index 0.
  • The array numbers have 5 elements, so the valid indices are 0 to 4.
  • Accessing numbers[5] tries to access the 6th element, which does not exist. This results in an ArrayIndexOutOfBoundsException.

 

Why Other Options Are Wrong:

A) 5: This would be correct if the code accessed numbers[4], which is the last element of the array.

B) 0: There is no logical reason for the output to be 0. This option is incorrect.

D) Compilation Error: The code is syntactically correct and will compile without errors. The error occurs at runtime.



 

One course. One step forward.

Your next chapter.
One course at a time.

Explore the available courses in this collection and choose where to focus your next study session.

Explore your courses