1 Programming In Java

Advanced Java Programming · Unit 1 · 8 hrs

Programming in Java

Exam-focused notes for Programming in Java (Advanced Java Programming, CSC419): what the TU syllabus asks and how it has actually been tested, with 25 solved past questions from this unit.

What this unit covers

  • Java Architecture
  • Java Buzzwords
  • Path and ClassPath variables
  • Sample Java Program
  • Compiling and Running Java Programs
  • Arrays
  • for each loop
  • Class and Object
  • Overloading
  • Access Privileges
  • Interface
  • Inner Class
  • Final and Static Modifiers
  • Packages
  • Inheritance
  • Overriding
  • Handling Exceptions: Try, Catch, Finally, Throws, and Throw keywords
  • Creating Exception Class
  • Concurrency: Introduction, Thread States, Writing Multithreaded Programs, Thread Properties, Thread Synchronization, Thread Priorities
  • Working with Files: Byte Stream Classes, Character Stream Classes, Random Access File, Reading and Writing Objects

Path and ClassPath variables

208110 marks

Differentiate between path and class path in Java. When do you prefer anonymous inner class? Consider the following program and answer the given questions. 1) Name the list of class file created after compiling above program 2) Rewrite the above program using for loop[10]

--- Feature PATH CLASSPATH --------- Definition A mediator between the developer and the operating system to inform the binary/executable file location A mediator between the developer and the compiler to inform the library file path used in source code Poi...

Full solved answer →

Working with Files

20815 marks

Assume that a text file named 'ONE.TXT' contains a paragraph of text. Write a program to copy the word that starts with vowel from 'ONE.TXT' to another file 'TWO.TXT'. [5]

- Read the content of ONE.TXT word by word - Check if each word starts with a vowel (a, e, i, o, u -- both uppercase and lowercase) - If yes, write that word to TWO.TXT --- --- --- Concept Detail ------ FileReader Reads characters from ONE.TXT BufferedReade...

Full solved answer →
20805 marks

Suppose that 9 integers are written in a file named 'magic.txt' in the arrangement of 3 x 3 separated by space. Write a program to check whether the integers in all rows, all columns, and both diagonals sum to the same constant or not. [5]

- Input file: magic.txt - Contains 9 integers arranged as a $3 \times 3$ grid, separated by spaces. - Task: Verify whether all 3 rows, all 3 columns, and both diagonals sum to the same constant (magic square check). No specific numeric values are given for ...

Full solved answer →
207710 marks

Describe the responsibility of Serializable interface. Write a program to read an input string from the user and write the vowels of that string in VOWEL.TXT and consonants in CONSONANT.TXT.[10]

The Serializable interface in Java is found in the java.io package. It is a marker interface, meaning it has no methods or fields. Its sole purpose is to mark a class so that the Java runtime knows the objects of that class can be serialized and deserialize...

Full solved answer →
20765 marks

Write a simple Java program that reads a file named 'Test.txt' and displays its contents. [5]

Java provides the java.io package for file handling. To read a file, we use FileReader wrapped with BufferedReader for efficient line-by-line reading. If the file is not found or cannot be read, an exception is thrown, which must be handled properly. --- --...

Full solved answer →

Concurrency

208010 marks

Why do we need to synchronize the thread? Justify with an example. An array with an odd number of elements is said to be centered if all elements (except the middle one) are strictly greater than the value of the middle element. Note that only arrays with an odd number of elements have a middle element. Write a function that accepts an integer array and returns 1 if it is a centered array, otherwise it returns 0.[10]

--- Thread synchronization is the mechanism that ensures that two or more concurrent threads do not simultaneously execute a critical section (a segment of code that accesses shared resources). Without synchronization, multiple threads accessing and modifyi...

Full solved answer →
20795 marks

What is multithreading? How can you create multithreaded program in Java? Explain. [5]

Multithreading is a feature of Java that allows concurrent execution of two or more parts (threads) of a program to maximize CPU utilization. A thread is a lightweight process that has its own files and stacks but can access shared data from other threads w...

Full solved answer →
20775 marks

Why synchronization is essential in multithreading? Describe. [5]

In a multithreaded environment, multiple threads share the same process resources (memory, data, objects). When two or more threads try to access and modify a shared object or variable simultaneously, it leads to data inconsistency and race conditions. Sync...

Full solved answer →
20755 marks

Write down the life cycle of thread. Write a program to execute multiple threads in priority base. [5]

State Description -------------------- New Thread object is created but start() is not yet called Runnable Thread is ready to run; waiting for CPU scheduling Running Thread is currently executing its run() method Blocked/Waiting Thread is waiting due to sle...

Full solved answer →

Class and Object

207910 marks

Explain feature of object-oriented programming. Create a class Distance with private variables feet of type integer and inches of type floating point. Use suitable constructor, and methods for adding and comparing two distance objects. [Hint: 1 feet = 12 inches][10]

--- OOP is a programming paradigm that organizes software as a collection of objects combining data and behaviour. The key features are: - A class is a blueprint or template that defines attributes (data) and methods (behaviour). - An object is a real-world...

Full solved answer →
20765 marks

Define class. How do you create a class in Java? Differentiate class with interface. [5]

--- A class in Java is a blueprint or template from which objects are created. It defines the properties (fields/attributes) and behaviors (methods) that the objects of that class will have. A class encapsulates data and the methods that operate on that dat...

Full solved answer →
20755 marks

Define the chain of constructor. What is the purpose of private constructor? [5]

--- Constructor chaining is the process of calling one constructor from another constructor within the same class or from a parent class. This creates a "chain" of constructor calls that executes in sequence. Constructor chaining is achieved using: - this()...

Full solved answer →

Sample Java Program

20795 marks

Write Java program that send messages with each other using TCP socket. [5]

TCP (Transmission Control Protocol) socket programming in Java uses the java.net package. Communication requires two programs: a Server and a Client. --- 1. Create object of ServerSocket class 2. Accept connection from client 3. Get input and output streams...

Full solved answer →

Final and Static Modifiers

20795 marks

Write short notes on: a. Final class b. Result set [2.5+2.5]

--- The final modifier in Java is used for finalizing the implementations of classes, methods, and variables. A final class is a class that is declared using the final keyword. Once a class is declared as final, it cannot be subclassed (inherited). This mea...

Full solved answer →
20755 marks

When do we use final method and final class? Differentiate between function overloading and function overriding. [5]

--- A final method is a method declared with the final keyword. It cannot be overridden by any subclass. When to use: - When you want to prevent a subclass from changing the implementation of a method. - When the method contains critical logic that must rem...

Full solved answer →

Java Architecture

207810 marks

Explain RMI architecture layers in detail. Write a Java programs using RMI to find product of two numbers.[10]

Remote Method Invocation (RMI) is a Java API that allows an object running in one Java Virtual Machine (JVM) to invoke methods on an object running in another JVM. It is the mechanism that makes Java truly distributed (as mentioned in Java's distributed fea...

Full solved answer →

Packages

20785 marks

What is package? How can you create your own package in Java? Explain with example. [5]

A package in Java is a namespace that organizes a set of related classes and interfaces together. It is essentially a folder/directory that groups related Java classes, interfaces, and sub-packages. Packages help in: - Avoiding name conflicts between classe...

Full solved answer →
20775 marks

What is the task of manifest file? Write the procedure to create it. [5]

A manifest file is a special metadata file included inside a JAR (Java Archive) file. It is named MANIFEST.MF and is located in the META-INF/ directory within the JAR. The main tasks of the manifest file are: - Specifies the Main Class: It tells the JVM whi...

Full solved answer →

Arrays

20775 marks

A non-empty array A of length n is called an array of all possibilities if it contains all numbers between 0 and A.length-1 inclusive. Write a method named isAllPossibilities that accepts an integer array and returns 1 if the array is an array of all possibilities, otherwise it returns 0. [5]

- Input: a non-empty integer array A of length n. - Condition: A must contain all integers from 0 to n-1 inclusive. - Output: return 1 if the condition holds, otherwise 0. (No specific test arrays are mandated by the question; behaviour must be general.) An...

Full solved answer →
20755 marks

An array is called balanced if it's even numbered elements (a[0], a[2], etc) are even and its odd numbered elements (a[1], a[3], etc) are Odd. Write a function named isBalanced that accepts an array of integers and returns 1 if the array is balanced otherwise it returns 0. [5]

This is a programming problem, not a numerical one. The requirements are: - Even-indexed elements (a[0], a[2], a[4], ...) must be even. - Odd-indexed elements (a[1], a[3], a[5], ...) must be odd. - Function name: isBalanced - Input: an array of integers (si...

Full solved answer →

Handling Exceptions

20775 marks

When does the finally block is mandatory in while handling exception? Describe with a suitable scenario. [5]

The finally block in Java is a block that always executes regardless of whether an exception occurs or not. It is used to place important cleanup code such as closing connections, releasing resources, or closing files. --- The finally block becomes mandator...

Full solved answer →
207610 marks

What is exception handling? Discuss the use of each keyword (try, catch, throw, throws and finally) with suitable Java program.[10]

Exception handling is a mechanism in Java that allows a program to handle runtime errors (exceptional conditions) in a controlled manner, so that the normal flow of the program can be maintained and the program does not crash abruptly. When an exception occ...

Full solved answer →

Inheritance

20775 marks

Why multiple inheritance is not allowed in Java using classes? Give an example. [5]

Java does not support multiple inheritance with classes primarily to avoid the Diamond Problem (also called the "Deadly Diamond of Death"). This is an ambiguity problem that arises when a class inherits from two or more classes that have a method with the s...

Full solved answer →

Compiling and Running Java Programs

20765 marks

Define Java Bean. How is it different from other Java programs? What is design pattern? [5]

A Java Bean is a reusable software component written in Java that follows a specific set of conventions (rules). It is essentially a Java class that encapsulates multiple objects into a single object (the bean). - The class must be public. - It must have a ...

Full solved answer →

Creating Exception Class

207510 marks

Why do we need to handle the exception? Distinguish error and exception. Write a program to demonstrate your own exception class.[10]

Exception handling is needed for the following reasons: 1. To maintain normal program flow: Without handling, an exception causes the program to terminate abruptly. Handling allows the rest of the code to continue executing. 2. To avoid program crash: Unhan...

Full solved answer →