0% found this document useful (0 votes)
2 views12 pages

Programming 2

The document provides a comprehensive overview of fundamental concepts in Java programming, including object-oriented programming principles, variable and constant definitions, control statements, and inheritance. Key topics covered include the syntax of variables, types of operators, loop structures, and the importance of access modifiers. Additionally, it explains concepts like method overloading and overriding, polymorphism, and information hiding, along with practical examples and explanations.

Uploaded by

shahmeerjabbar66
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views12 pages

Programming 2

The document provides a comprehensive overview of fundamental concepts in Java programming, including object-oriented programming principles, variable and constant definitions, control statements, and inheritance. Key topics covered include the syntax of variables, types of operators, loop structures, and the importance of access modifiers. Additionally, it explains concepts like method overloading and overriding, polymorphism, and information hiding, along with practical examples and explanations.

Uploaded by

shahmeerjabbar66
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

UNIT NO:1 SHORT ANSWERS

Q1. What is oop?

Object-oriented programming (OOP) is a way of writing code by making "objects" that store information
and do things. It helps keep the code simple and easy to understand.

Q2. When was java introduced?

Java was introduced in 1995 by Sun Microsystems (which was later acquired by Oracle). It was designed
to be a platform-independent language, meaning it could run on different types of computers without
needing to be rewritten.

Q3. Who made java?

Java was created by James Gosling and his team at Sun Microsystems in 1991. James Gosling is often
called the "father of Java

Q4. What do you know about the security of java?

Java has strong security features like sandboxing, bytecode verification, and access control to protect
against malicious activities. It also includes tools for encryption and secure data handling

Q5.What are program execution phases?

1. Writing 2. Compilation 3. Linking 4. Loading 5. Execution 6. Termination

Q6.What is flyte code?

Flyte is a tool that helps you create and run complex tasks, like data processing or machine learning,
automatically on the cloud. You write code (usually in Python) to define these tasks and how they work
together in a workflow.

Q7.How to create a new project in NetBeans?

File > New Project > Choose Project Type > Name Project > Finish

Q8.How to add class or package in java program?

To add a class or package in a Java program:

1. **Add a Class**:

Right-click on the `src` folder > New > Java Class > Enter class name > Click Finish.

2. **Add a Package**:

Right-click on the `src` folder > New > Package > Enter package name > Click Finish.

Q9.How to build a project in java?

To build a project in Java:

Using NetBeans:
Click on the project in the Projects window, then go to Run > Build Project or press F11.

Using Command Line:

Navigate to the project directory and run

Q10.What is package?

A package in Java is a way to organize related classes and interfaces into a folder-like structure. It helps
group similar classes together, making code easier to manage, maintain, and avoid naming conflicts. For
example, java.util is a package that contains utility classes like ArrayList and HashMap.

Q1What is class?

A class in Java is a blueprint or template for creating objects. It defines properties (attributes) and
behaviors (methods) that the objects created from the class will have. For example, a Car class might
have properties like color and speed, and behaviors like drive() and brake().

Q12.Define class name?

A class name in Java is the identifier used to represent a class. It typically starts with an uppercase letter
and follows camel case (e.g., Car, StudentRecord, EmployeeDetails). The class name is used to create
objects and to refer to the blueprint that defines the properties and behaviors of those objects.

Q13.What are data member?

**Data members** (also called **fields** or **attributes**) in a class are variables that hold the state
or properties of an object. They represent the data that an object will store. For example, in a `Car` class,
data members might include `color`, `model`, and `speed`. These are specific to each object created
from the class.

Q14.Define methods?

Methods in Java are functions defined inside a class that represent the actions or behaviors an object
can perform. They can take input (parameters), perform some task, and return a value. For example, a
Car class might have methods like drive(), brake(), or accelerate(), which define what actions the car can
perform

Q15.What are comments?

Comments in Java are non-executable lines of text used to explain and document the code. They are
ignored by the compiler and help programmers understand the code or leave notes for others.

Q16.Define single line comments?

A single-line comment in Java is used to add a note or explanation to a single line of code. It begins
with //, and everything after // on that line is ignored by the compile

Q17.Define multiline comments?

Multi-line comments in Java are used to add comments that span multiple lines. They begin with /* and
end with */. Everything between these symbols is ignored by the compiler
UNIT: 2 VARIABLE AND CONSTANTS

Q.1 What is the syntax of variable?

Apologies for the confusion! Here's the syntax for declaring a variable in many
programming languages:

<variable_name> = <value>

Q.2 Define and explain Identifier?

An identifier is a name used to identify variables, functions, classes, or other entities


in programming, and it must start with a letter or underscore and can contain
letters, numbers, and underscores.

Q.3 Define literals?

In Java, literals are constant values that appear directly in the code, such as
numbers, characters, or strings. They represent data used by variables or
operations, like 5, 'A', or "Hello".

Q.4 Define Keywords?

In Java, keywords are predefined reserved words that have a specific meaning and
cannot be used as identifiers. Examples include class, public, static, and int, which
define the structure and behavior of the program.

Q.5 Define Relational Operator?

Relational operators in Java are used to compare two values, returning true or false
based on the result. Examples include ==, !=, <, >, <=, and >=.

Q.6 Define Logical Operator?

Logical operators in Java are used to perform logical operations on boolean values,
returning true or false. The common logical operators are && (AND), || (OR), and !
(NOT).

Q.7 Define Bitwise operator?

Bitwise operators in Java perform operations on the individual bits of integer values.
Common bitwise operators include & (AND), | (OR), ^ (XOR), ~ (NOT), << (left
shift), and >> (right shift).

Q.8 Define Increment operator?

The increment operator (++) in Java increases the value of a variable by 1. It can be
used in two forms: prefix (++x) and postfix (x++), affecting when the increment
happens relative to the expression.
Q.9 Define Decrement operator?

The decrement operator (--) in Java decreases the value of a variable by 1. It can be
used as a prefix (--x) or postfix (x--), determining when the decrement occurs.

Q.10 What is prefix operator?

A prefix operator (++x or --x) in Java increments or decrements the value of a


variable before it is used in an expression.

Example:

int x = 5;

int y = ++x; // x is incremented first, then y becomes 6

Q.11 What is postfix operator?

A postfix operator (x++ or x--) in Java increments or decrements the value of a


variable after it is used in an expression.

Example:

int x = 5;
int y = x++; // y becomes 5, then x is incremented to 6
Q.12 What is prefix decrement operator?

The prefix decrement operator (--x) in Java decreases the value of a variable by 1
before it is used in an expression.

Example:

int x = 5;
int y = --x; // x is decremented to 4, then y becomes 4

Q.13 Define Assignment Operator?

An assignment operator (=) in Java is used to assign a value to a variable. It stores


the value on the right into the variable on the left.

Example:

int x = 10; // Assigns 10 to the variable x

Q.14 Define Unary operator?

A unary operator in Java is an operator that works with only one operand to perform
operations like increment, decrement, or negation.
Examples:

 ++x (increment)
 --x (decrement)
 -x (negation)

Q.15 Define ternary operator?

A ternary operator in Java is a shorthand for an if-else statement, using three


operands. It has the form: condition ? value_if_true : value_if_false.

Example:

int x = 10;
int result = (x > 5) ? 100 : 200; // If x is greater than 5, result is 100, else 200

Q.16 What is Modulus operator?

The modulus operator (%) in Java returns the remainder of a division between two
numbers.

Example:

int result = 10 % 3; // result is 1, as 10 divided by 3 leaves a remainder of 1


UNIT : 4 CONTROL STATEMENTS

Q.1 What is infinite while loop in java?

An infinite while loop in Java is a loop that keeps running endlessly because its condition is
always true. For example:

while (true) {

// Code here keeps running forever


}
Q.2 What is the importance of java for each loop?

The for-each loop in Java is used to easily go through all items in an array or list without
needing an index. Example:

for (int num : numbers) {


// Use each item
}
Q.3 Is there a way to access an iteration counter in java for each loop?

No, the for-each loop in Java doesn’t have a direct way to access an iteration counter. To track
the index, use a regular for loop instead. Example:

for (int i = 0; i < numbers.length; i++) {

// Use i as the counter

}
Q.4 Advantages of declaring loop variable as final in enhanced
for-loop?

Advantages of declaring loop variable as final in enhanced for-loop:

 Prevents accidental modification of the variable.


 Ensures immutability, making the code safer and easier to debug.
 Indicates the variable is read-only for better clarity.

Q.5 Does returning a value from the loop body break the loop?

Yes, returning a value from the loop body immediately exits the loop and the entire method, stopping
further iterations.
Q.6 What happen when we forget break in switch case?
If you forget the break in a switch case, the code will "fall through," meaning it will keep
executing the next cases until it finds a break or the switch ends.

Q.6 How will you exit anticipatedly form a loop?

You can exit a loop early using the break statement, which stops the loop immediately when a
condition is met

Q.7 What is flow chart?

A flowchart is a diagram that shows the steps in a process using shapes like arrows, circles, and
rectangles. It helps to visualize how something works.

Q.8 What can do break statement in switch case?

In a switch case, the break statement stops the execution of the current case and exits the
switch, preventing it from running the next cases.

Q.9 Difference between dead code and unreachable code in java?

 Dead Code:
1. Code that will never run, usually because it's placed after a return or an infinite
loop.
2. It serves no purpose and should be removed to improve code quality.
 Unreachable Code:
1. Code that cannot be executed due to the program’s flow (like after a break,
return, or throw).
2. The compiler usually flags unreachable code as an error to indicate a logical
mistake.

Q.10 Which is considered as a selection statement?

A selection statement helps you choose what to do based on a condition, like if or switch.

Q.11 What is the difference between while and do while loop?

 While loop: The condition is checked before the loop starts, so the code may not run at
all if the condition is false.
 Do-while loop: The code runs at least once, as the condition is checked after the loop
runs.

Q.12 Can we write switch case using a double variable?

No, in Java, you cannot use a double variable in a switch case. Switch cases only support variables of
types like int, char, byte, short, String, and enum.
UNIT: 5 OBJECT ORIENTED PROGRAMMING

ANSWER THE SHORT ANSWER:


Q.1 How inheritance is implemented in java?
Inheritance in Java allows a class (child) to use properties and methods of another class (parent)
using the extends keyword. This helps in code reusability and establishing a relationship
between classes.
Q.2 Are static members inherited to subclasses?

Static members are not inherited by subclasses because they belong to the class, not instances.
However, subclasses can access them using the parent class name.

Q.3 What happens if the parent and the child class have a field with same identifier?

If the parent and child class have a field with the same name, the child’s field hides the parent’s
field. To access the parent’s field, use super.fieldName.

Q.4 Are constructors and initializer also inherited to subclass?

No, constructors and initializers are not inherited by subclasses, but the child class can call the
parent’s constructor using super().

Q.5 How do you restrict a member of a class from inheriting by its subclasses?

To restrict a member from being inherited, declare it as private, so it’s accessible only within
the same class. You can also use final for methods to prevent overriding.

Q. 6 How do you implement multiple inheritance in java?

Java does not support multiple inheritance with classes, but it can be achieved using interfaces.
A class can implement multiple interfaces using the implements keyword.

Q. 7 Can a class extend by itself in java?

No, a class cannot extend itself in Java because it would create an infinite inheritance loop,
Wqhich is not allowed.

Q. 8 Can we reduce the visibility of the inherited or overridden method?


No, we cannot reduce the visibility of an inherited or overridden method; it must be the same
or more accessible. For example, a public method in the parent cannot be changed to private in
the child.

Q. 9 How do you override a private method in java?

A private method cannot be overridden because it is not accessible outside its own class.
However, a subclass can define a new method with the same name, but it will be a separate
method, not an override.

Q. 10 Why use inheritance in java?

Inheritance in Java is used to reuse code, reduce redundancy, and establish a relationship
between classes. It helps in better code organization and makes maintenance easier.

Q.11 Why multiple inheritance not supported in java?

Multiple inheritance is not supported in Java to avoid conflicts when two parent classes have
the same method (Diamond Problem). This prevents ambiguity and keeps the code simple and
clear.

Q. 12 What is compile time polymorphism or static polymorphism?

Compile-time polymorphism (also called static polymorphism) is a type of polymorphism where


method overloading or operator overloading is resolved at compile time. It allows multiple
methods with the same name but different parameters to exist in a class.

Q. 13 What is run time polymorphism or dynamic polymorphism?

Runtime polymorphism (also called dynamic polymorphism) is a type of polymorphism where


method overriding is resolved at runtime. It allows a subclass to provide a specific
implementation of a method that is already defined in its parent class.
Q.14 What is method overloading?
Method overloading means having multiple methods with the same name but different inputs
in a class. It helps perform different tasks using the same method name.

Q. 15 What is method overriding?


Method overriding means redefining a method in a child class that already exists in the parent
class. It allows the child class to provide its own specific implementation of the method.

Q. 16 What are three ways to overload a method?


There are three ways to overload a method in Java or other object-oriented languages:
Changing the number of parameters
Changing the type of parameters
Changing the order of parameters
Q. 17 What is the invalid case of method overloading?
An invalid case of method overloading occurs when only the return type is changed while
keeping the same method name and parameters. The compiler does not consider return type
for method overloading.
Q. 18 What is type promotion?
Type promotion in Java automatically converts a smaller data type to a larger data type to
prevent data loss. For example, an int can be promoted to a double in arithmetic operations.
Q. 19 What is the advantage of method overriding?

 It allows a subclass to provide a specific implementation of a method from the parent


class.
 It enables runtime polymorphism, improving flexibility and maintainability.
 It enhances code reusability by modifying existing behavior without changing the
original code.

Q. 20 What are access modifiers?

Access modifiers are keywords in object-oriented programming that control the visibility of
classes, methods, and variables. They include public, private, protected, and default, defining
how accessible a member is within or outside a class.

Q. 21 What is the advantages of polymorphism?

 It allows one interface to be used for different data types or methods.


 It improves code flexibility and scalability by enabling method overloading and overriding.

 It enhances maintainability by reducing code duplication and increasing reusability.

Q. 22 What is information hiding?

nformation hiding is the practice of restricting direct access to an object's data and only allowing
controlled interaction through methods. This helps in improving security, reducing complexity, and
making the code easier to maintain.

Q. 23 Give any real life example of method overriding?

A real-life example of method overriding is a Bank's Interest Rate System, where a parent class Bank
has a method getInterestRate(). Different banks like HBLBank and MeezanBank override this
method to provide their specific interest rates.

Q. 24 What is encapsulation?

Encapsulation is the process of wrapping data and methods inside a class while restricting direct access
to the data. It enhances security, reduces complexity, and allows controlled access through getters and
setters.
Q. 25 How we can achieve encapsulation in java?

We achieve encapsulation in Java by declaring variables as private and providing public getter and
setter methods to access and modify them, ensuring controlled data access.

Q. 26 What is abstraction?

Abstraction is the process of hiding implementation details and showing only the essential
features of an object. In Java, it is achieved using abstract classes and interfaces to simplify
complex systems.

Q. 27 What are abstract classes?

An abstract class in Java is a class that cannot be instantiated and is used to provide a base for other
classes. It can have both abstract (unimplemented) and non-abstract (implemented) methods,
allowing subclasses to define specific behaviors.
UNIT : 8 EXCEPTION HANDLING

Q.1 How to handle exceptions in java?


In Java, exceptions are handled using try and catch blocks. The try block holds the code that might cause
an error, and the catch block handles the error to prevent the program from crashing.

Q.2 What is the difference between exceptions and error in java?

In Java, exceptions are problems that occur during program execution and can usually be handled by the
program, like input errors or file not found.
Errors are serious problems like system crashes or memory issues, and they cannot be handled by the
program easily.

Q.3 Can we throw in exception manually?

Yes, we can manually throw an exception in Java using throw. It’s used to trigger custom errors
during code execution.

You might also like