JAVA : Chapter 1 : EXAM PREPARATION : TYBCS : SPPU

 Chapter 1 An Introduction to Java 

1 m

Q. What is JAR file?

A JAR (Java ARchive) file is a compressed archive format used in Java to package multiple files, including Java class files, resources, and metadata, into a single file.

Q. What is an applet?

Answer: An applet is a java program that runs in a web browser. It is designed to be included in HTML document.

Q. What is java?

- Java is a high-level object oriented programming language. It combines both data and functions that operate on data into a single unit (i.e. an object).

Q. What is final variable?

Answer: Final variable is a variable with a value that cannot be modified during the execution of the program.

It is declared with following syntax:

final data_type varName=value;


Q. What is JDK?

- JDK (Java Development Kit) is a software development environment used to develop java applications.

Q. What is JRE?

It is a set of software tools used to develop java applications. It provides runtime environment. Java Runtime Environment.

- c) What is javadoc? 

- Javadoc is a tool in Java used for generating documentation. Documentation is made using special comments within the source code. It contains clear explanations of classes, methods, and their usage. The generated documentation helps in code understanding and usage by other developers.


(b) List primitive data types in Java with their width in bits. 

- Following are the primitive data types in Java with their width in bits:

byte: 8 bits

short: 16 bits

int: 32 bits

long: 64 bits

float: 32 bits

double: 64 bits

char: 16 bits

boolean: Not strictly defined, but typically represented as 8 bits (1 byte)


(a) What do you mean by jdb ? 

jdb stands for Java Debugger, which is a command-line debugging tool that comes with the Java Development Kit (JDK). 

It's used by Java developers to find and fix errors (bugs) in their Java programs. 


Q. What is Object?

It is an instance of class which has it's unique data .

Q. What is Class?

It is a blueprint that defines structure and behaviour of objects.

e) What is Javadoc comments? 

- Javadoc comments are special comments in Java code that are used to create documentation for classes, methods, fields, and other program elements.Javadoc comments start with /** and use specific tags (e.g., @param, @return, @throws) to structure and organize the information.


f) What is command line argument?

- A command-line argument in Java is input data that we pass to a program when running it from the command line or terminal. These arguments are accessed through the args parameter in the main method and allow us to customize the program's behavior without changing its code.


(a) What is the use of javap tool ?

- The javap tool in Java is used to disassemble compiled .class files, revealing the bytecode instructions that make up the Java code. It helps developers understand how Java code is translated into bytecode, providing insights into the low-level details of the program's structure, methods, fields, and interfaces.


(b) What is blank final variable ? 

-

A blank final variable in Java is a variable that is declared as final but is not assigned a value when it is declared. Instead, it is assigned a value only once, typically in the constructor of the class or in an instance initialization block. Once a value is assigned to a blank final variable, it cannot be changed again.

 (c) “Java is platform dependant language.” True/False ? Justify.

Answer: False. Java is often described as a platform-independent or "write once, run anywhere" language. This is due to its ability to run on any platform that has a compatible Java Virtual Machine (JVM). Here's the justification:


Q. Name any four tags of Javadoc.

Answer: Following are the tags of javadoc:

@param: Used to describe the parameters of a method , explaining their purpose and expected values.

@return: Describes the value that a method returns, explaining its meaning and possible outcomes.

@throws : Specifies the exceptions that a method might throw and provides details about when these exceptions can occur.

@see: Provides links to other related classes, methods, or fields that developers might find useful when using or understanding the current code.


 4 mark

Q. Explain the process of compilation of Java program.

Answer: 

1. The java code is written using text editor which contains instructions that program will execute,

2. Once ready, we use Java Compiler using javac file_name command in command line.

3. Javac compiler creates a file named filename.class which contians bytecode version of the program.

4. This bytecode file is loaded into memory using JVM which interprets the bytecode and executes the programs logic step by step.

 

a) Write a Java program to accept a number from user. If it is zero then throw user defined exception "Number is zero". Otherwise calculate its factorial.

Q. Explain how the input is accepted from command line with the help of program.

Answer: The Java command line argument is an argument passed at runtime. The arguments passed from console can be received in java program and it can be used as an input.

The argument is passed to main() from the command line and these areguments are stored in parameter of main() which is an array of string objects, i.e. args[].

Program: 

public class commdLineArg

{

    public static void main(String args[])

    { 

        System.out.println("Total arguments passed are" + args.length);

        System.out.println("Each of them are");

        for(int k=0; k<args.length; k++)

        System.out.println ("arg["+k+"]:" + args [k]);

   } 

}

Output:

java commdLineArg I am a girl

Total Arguments passed are 4

Each of them are:

arg[0] : I

arg[1]: am

arg[2]: a

arg[3]: girl

Here we use public static void main(String args[]) to receive the arguments passed in command line.

The arguments supplied are of string type in array of string objects.


Q. Write a java program to find second smallest element in array.

- Answer: 

      
        public class SecondSmallestElement {
            public static void main(String[] args) {
                int[] array = {5, 2, 8, 1, 3, 7}; // Replace this with your array
                
                if (array.length < 2) {
                    System.out.println("Array should have at least two elements.");
                    return;
                }
        
                int smallest = Integer.MAX_VALUE;
                int secondSmallest = Integer.MAX_VALUE;
        
                for (int num : array) {
                    if (num < smallest) {
                        secondSmallest = smallest;
                        smallest = num;
                    } else if (num < secondSmallest && num != smallest) {
                        secondSmallest = num;
                    }
                }
        
                if (secondSmallest == Integer.MAX_VALUE) {
                    System.out.println("No second smallest element found.");
                } else {
                    System.out.println("The second smallest element is: " + secondSmallest);
                }
            }
        }
      
    


Q. Explain various ways to read input string from console or keyboard.

Answer: Java provides following ways to accept input from the user:

1) Using BufferedReader Class: The java.io.BufferedReader class reads text from a character-input stream.

Class declaration: 

public class BufferedReader

extends Reader

Class constructors:

1. BufferedReader(Reader in) : It uses default-sized input buffer.

2. BufferedReader(Reader in, int sz): It uses an input buffer of specified size.

Class Methods:

1. int read(): Reads single character.

2. String readLine(): Reads a line of text.

3. long skip(long n) : Skips characters.


2) Using Command Line Arguments: It is the argument passed at time of running a java program. 

The arguments are stored in parameter of main() which is an array of string objects - args[].

public static void main(String args[]) is used to receive argument.


3) Using Scanner Class: It can be used to read input from user by importing java.util.Scanner package.

Class Methods: 

1. int nextInt(): Scans next token of input as integer.

2. String nextLine(): Scans the currrent line.

3. byte nextByte(): Scans the next token of input as a Byte.


b) Explain the features of Java.

:- Following are the features of Java:

1. Simple: It is a simple language and easy to learn since it dosen't use concepts like pointers, preprocessors, headerfiles, etc.

2. Object Oriented: All the program code in java is present within objects and classes.

3. Platform Independent: It can run on multiple platforms like windows, Linus, Mac Os, etc.

4. Interpreted: Java is an interpreted language i.e. programs run directly from the source code. 

5. Dynamic: It can dynamically link in new class libraries, methods and objects at runtime.

6. Secure: It does not allocate direct pointer to memory which makes it very secure. 

7: Arcihtecture Neutral: Java compiler generates object file which is executable on many processors.

8: Portable: Java environment can be easily ported to different architectures,

9: High performance: It uses JIT (Just in Time) Compilers which increases its performace.

10. Multithreading: We can write programs that can perform many tasks simultaneously.


 Q. Why is Java called purely object oriented programming language ? Explain features of Java.

Answer: Java is called purely object oriented programming language because:

Everything in Classes: In Java, all code is organized within classes. 

Objects Everywhere: Java focuses on using objects as the building blocks of programs. 

Inheritance and Polymorphism: Java allows classes to inherit properties from other classes and enables objects to be treated as instances of their parent classes, promoting code reuse.

Abstraction with Interfaces:In Java, we can make a plan called an "interface" that describes what a group of classes should be able to do. This helps keep different parts of a program separate and makes it easier to work on one thing at a time.