KEMBAR78
Unit 1 | PDF | Programming | Constructor (Object Oriented Programming)
0% found this document useful (0 votes)
12 views11 pages

Unit 1

The document is a question bank for a Java programming course, covering fundamental concepts such as features of Java, differences between JDK, JRE, and JVM, data types, variables, operators, type conversion, flow control statements, and object-oriented programming principles. It includes both theoretical questions and practical coding exercises to reinforce learning. Additionally, it outlines the structure of Java classes, methods, constructors, encapsulation, and access control.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views11 pages

Unit 1

The document is a question bank for a Java programming course, covering fundamental concepts such as features of Java, differences between JDK, JRE, and JVM, data types, variables, operators, type conversion, flow control statements, and object-oriented programming principles. It includes both theoretical questions and practical coding exercises to reinforce learning. Additionally, it outlines the structure of Java classes, methods, constructors, encapsulation, and access control.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA SCIENCES

24PIT302 – JAVA PROGRAMMING


QUESTION BANK

UNIT I - INTRODUCTION TO JAVA

PART A

1. What are the features of java?

1. Simple
2. Object-Oriented
3. Portable
4. Platform independent
5. Secured
6. Robust
7. Architecture neutral
8. Interpreted
9. High Performance
10. Multithreaded
11. Dynamic

2. Difference between JDK, JRE, and JVM.

JDK is abbreviated as Java Development Kit which has a physical existence. It can be
considered as a kit inside which resides the JRE along with developing tools within it. The
programmers and developers mostly use it.

JVM is abbreviated as Java Virtual Machine, is basically a dummy machine or you can say
an abstract machine which gives Java programmers a runtime environment for executing the
Bytecode. For each execution of your program, the JDK and JRE come into use, and they go
within the JVM to run the Java source code.

JRE is abbreviated as Java Runtime Environment, as the name suggests used as a package
that gives an environment to run the Java program on your machine.

3. What is bytecode?

Bytecode is a highly optimized set of instructions designed to be executed by the java run-
time system. Which is called the java virtual machine (JVM). JVM is an interpreter for
bytecode.

4. What is a variable? What are the different types of variables?

Variable are locations in the memory that can hold values. Java has three kinds of variable

namely,
Instance variable

Local variable

Class variable

Local variables are used inside blocks as counts or in methods as temporary variables. Once
the block or the method is executed, the variable ceases to exist. Instance variable are used to
define attributes or the state of a particular object. These are used to store information needed
by multiple methods in the objects.

5. What are the difference between static variable and instance variable?

The data or variables, defined within a class are called instance variables.
Instance variables declared as static are, essentially, global variables. When objects of its
class are declared, no copy of a static variable is made.

6. .What are primitive datatypes in java?

There are 8 types of primitive data types:

1. boolean data type


2. byte data type
3. char data type
4. short data type
5. int data type
6. long data type
7. float data type
8. double data type

7. List out the operator in Java.

1. Arithmetic Operators
2. Increment and Decrement Operators
3. Bitewise Operators
4. Relational Operators
5. Logical Operators
6. Assignment Operators

8. What is type conversion in Java?

Type conversion in Java is the process of converting a variable from one data type to another.
It occurs in two forms:

Implicit Conversion (Widening) – Done automatically by the compiler when converting from
a smaller to a larger data type.

Explicit Conversion (Narrowing or Type Casting) – Manually done by the programmer when
converting from a larger to a smaller data type using a cast operator.
9. Give an example of implicit type conversion in Java.

int a = 100;

long b = a; // int is automatically converted to long

Here, a (int) is automatically converted to b (long), which is a widening conversion.

10. Give an example of explicit type casting in Java.

double d = 5.75;

int i = (int) d; // explicit casting

Here, d is explicitly cast to int. The decimal part is truncated, and only the integer value is
stored in i.

11. What is the syntax of type casting in Java?

The syntax for type casting (explicit conversion) is:

targetType variableName = (targetType) value;

Example: int i = (int) 12.56;

12. Is data loss possible during type casting? Why?

Yes, data loss is possible during explicit type casting (narrowing conversion). When
converting from a larger or more precise data type (e.g., double) to a smaller or less precise
type (e.g., int), precision or value may be lost.

Example:

double d = 10.99;
int i = (int) d; // i becomes 10, fractional part lost

13. What happens when a float is cast to an int?

When a float is cast to an int, the decimal part is truncated, not rounded.

Example:

float f = 9.99f;
int i = (int) f; // i = 9

14. What are flow control statements in Java?

Flow control statements in Java are used to control the order of execution of statements or
blocks of code based on conditions or repeated execution (looping).

Types include:
Decision-making statements (e.g., if, switch)

Looping statements (e.g., for, while, do-while)

15. What is the purpose of the if-else statement in Java?

The if-else statement is used to execute one block of code if a condition is true, and another
block if it is false.

Example:

if (num > 0) {
System.out.println("Positive");
} else {
System.out.println("Non-positive");
}

16. What is the use of the break statement in loops?

The break statement is used to terminate a loop or switch statement immediately, skipping the
remaining iterations or cases.

Example:

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


if (i == 5) break;
System.out.println(i);
}

17. How do you read input from the keyboard in Java?

To read input from the keyboard, Java provides the Scanner class in the java.util package.

Example:

import java.util.Scanner;
Scanner sc = new Scanner(System.in);
int age = sc.nextInt(); // reads integer input

18. What is the Scanner class in Java?

The Scanner class is used to read user input from various sources, including the keyboard
(System.in). It supports multiple data types like int, double, String, etc.

Example:

Scanner sc = new Scanner(System.in);

String name = sc.nextLine();

19. What is the purpose of nextInt() and nextDouble() methods in Scanner?


These methods read numeric input from the user:

nextInt() reads an integer

nextDouble() reads a double (decimal number)

Example:

int age = sc.nextInt();

double price = sc.nextDouble();

20. What are command-line arguments in Java?

Command-line arguments are inputs passed to a Java program when it starts execution.

They are received in the main() method via the String[] args array.

public static void main(String[] args) {

System.out.println(args[0]); // prints first argument

21. Write a code snippet to read two numbers using Scanner and print their sum.

Scanner sc = new Scanner(System.in);


int a = sc.nextInt();
int b = sc.nextInt();
System.out.println("Sum: " + (a + b));

22. What is an Array in java?

An array is a very common type of data structure wherein all elements must be of the same
data type. Once defined, the size of an array is fixed and cannot increase to accommodate
more elements.The first element of an array starts with index zero.

23. Explain the different ways of using arrays?

1) Declaring Array

2) Constructing Array

3) Initialize Array

1) Declaring Array

Syntax

<elementType>[] <arrayName>;

OR
<elementType><arrayName>[];

Example:

intintArray[];

// Defines that intArray is an ARRAY variable which will store integer values

int[]intArray;

2) Constructing an Array

arrayname = new dataType[]

Example:

intArray = new int[10]; // Defines that intArray will store 10 integer values

Declaration and Construction combined

intintArray[] = new int[10];

3) Initialize an Array

intArray[0]=1; // Assigns an integer value 1 to the first element 0 of the array

intArray[1]=2; // Assigns an integer value 2 to the second element 1 of the array

24. Define class?

A class is a user defined blueprint or prototype from which objects are created. It represents
the set of properties or methods that are common to all objects of one type. In general, class
declarations can include these components, in order:
 Modifiers : A class can be public or has default access
 Class name: The name should begin with a initial letter (capitalized by convention).
 Superclass(if any): The name of the class’s parent (superclass), if any, preceded by
the keyword extends.
A class can only extend (subclass) one parent.
 Interfaces(if any): A comma-separated list of interfaces implemented by the class, if
any, preceded by the keyword implements. A class can implement more than one
interface.
 Body: The class body surrounded by braces, { }.

25. Define object?

It is a basic unit of Object Oriented Programming and represents the real life entities.
Atypical Java program creates many objects, interact by invoking methods. An object
consists of :
 State : It is represented by attributes of an object. It also reflects the properties of an
object.
 Behavior : It is represented by methods of an object. It also reflects the response of an
object with other objects.
 Identity : It gives a unique name to an object and enables one object to interact with
other objects.

26. How will you create method in java ?

A Java method is a collection of statements that are grouped together to perform anOperation
method definition consists of a method header and a method body. The same is shown in the
following syntax −

Syntax

modifier returnTypenameOfMethod (Parameter List)


{
// method body
}

 modifier − It defines the access type of the method and it is optional to use.
 returnType − Method may return a value.
 nameOfMethod − This is the method name. The method signature consists of the
method name and the parameter list.
 Parameter List − The list of parameters, it is the type, order, and number of
parameters of a method. These are optional, method may contain zero parameters.
 Method body − The method body defines what the method does with the statements.

27. Define method?

Methods are functions that operates on instances of classes in which they are defined. Objects
can communicate with each other using methods and can call methods in other classes. Just
as there are class and instance variable, there are class and instance methods. Instance
methods apply and operate on an instance of the class while class methods operate on the
class.

28. Explain the purpose of constructor?

Constructors are used to initialize the object’s state. Like methods, a constructor also
contains collection of statements that are executed at time of Object Creation.

29. When is a Constructor called?

Each time an object is created using new() keyword at least one constructor (it could
bedefault constructor) is invoked to assign initial values to the data members of the same
Class. Constructor is invoked at the time of object or instance creation.
30. Explain the types of constructors in java?

There are two type of constructor in Java:

 No-argument constructor: A constructor that has no parameter is known as default


constructor. If we don’t define a constructor in a class, then compiler creates default
constructor(with no arguments) for the class.
 Parameterized Constructor: A constructor that has parameters is known as
parameterized constructor. If we want to initialize fields of the class with your own
values, then use parameterized

31. How constructors are different from methods in Java?

 Constructor must have the same name as the class within which it defined while it is
not necessary forthe method in java.
 Constructor does not any return type while method(s) have the return type or void if
does notreturn any value
 Constructor is called only once at the time of Object creation while method(s) can be
called any numbers of time.

32. Explain the purpose of java static variable?

The static variable can be used to refer the common property of all objects (that is not unique
for each object

The static variable gets memory only once in class area at the time of class loading.

Advantage of static variable:

It makes your program memory efficient (i.e. it saves memory).

33. Explain java static method?

 A static method belongs to the class rather than object of a class.


 A static method can be invoked without the need for creating an instance of a class.
 Static method can access static data member and can change the value of it.

34. What is the purpose of the this keyword in Java?

The this keyword is a reference variable that refers to the current object. It is commonly used
to:

 Differentiate between instance variables and parameters with the same name
 Call other constructors in the same class (this())
 Pass the current object as an argument

35. Can this keyword be used to invoke constructors? Give an example.

Yes, this() can be used to call another constructor in the same class.
Example:

class Book {
Book() {
this("Unknown"); // calls parameterized constructor
}
Book(String title) {
System.out.println("Title: " + title);
}
}

36. Define encapsulation in Java.

Encapsulation is the process of binding data and methods that operate on the data into a
single unit (class), and restricting access to some of the object's components. It is achieved
using:

 private variables
 public getter/setter methods

37. What is access control in Java?

Access control is a mechanism to restrict access to class members (variables and methods)
using access modifiers:

private: Accessible within the same class only

default (no modifier): Accessible within the same package

protected: Accessible within the same package and subclasses

public: Accessible from anywhere

38. Can this keyword be used in static methods? Why or why not?

No, this cannot be used in a static method because this refers to the current instance, and
static methods belong to the class, not to any specific instance.

PART B

1. Explain the architecture of Java and discuss the role of JVM, JRE, and JDK with diagrams.

2. Describe in detail the features of Java that make it platform-independent and secure.

3. Discuss the different data types, variables, and operators in Java with suitable examples.

4. Explain the type conversion and casting mechanisms in Java. Illustrate with code snippets.

5. Write a Java program to demonstrate flow control statements including if-else, switch,
while, and for loops.
6. Illustrate how input is read from the keyboard using Scanner class and command-line
arguments with sample programs.

7. Explain arrays in Java. Write a program to perform operations on one-dimensional and


two-dimensional arrays.

8. Define class and object in Java. Explain with an example. Also, represent it using a UML
class diagram.

9. Discuss methods, constructors, static members, and the use of the this keyword with
examples.

10. What is encapsulation? How is access control implemented in Java? Illustrate using a
Java class example.

11. Explain about the Constructors and its type with example program.

PART - C

1. Write a java program with nested try statements that raises divide by zero exception and
out of bound exception, if the program contains a statement with division operator and a
divisor as a command line argument.

2. Write a Java Program to accept ‘n’ names, store it in an array, sort the names in alphabetic
order and display the result. Use classes and methods.

3. Write a Java Program to accept two square matrices, store them in an array, add the
matrices and display the result. Use classes and methods.

4. Write a Java program to read two integers from the keyboard using the Scanner class.
Perform arithmetic operations (+, -, *, /, %) on them and display the results.

5. Create a Java class called Student with data members: name, roll number, and marks (as an
array for 5 subjects). Include:

 A constructor to initialize values


 A method to calculate average marks
 A method to display student details

6. Write a Java program that takes a number from the command line and checks whether it is
a prime number or not.

7. Develop a Java class called Rectangle with length and breadth as data members. Include:

 A constructor for initialization


 A method to calculate area
 A method to display dimensions and area
8. Create a class BankAccount with fields: account holder name, account number, and
balance.

 Provide appropriate constructors


 Add deposit and withdrawal methods
 Use access control (private) and public methods to access them

9. Create a class Employee with static variable companyName.

 Create multiple Employee objects with different names and salaries


 Use constructor to initialize
 Display employee details and company name

You might also like