0% found this document useful (0 votes)
1 views31 pages

Prgramming With Java Notes

The document provides a series of sample short questions and answers related to Java programming, covering topics such as Object Oriented Programming (OOP), Java components (JVM, JRE, JDK), classes and objects, control statements, and key concepts like inheritance, polymorphism, encapsulation, and abstraction. Each section includes definitions, examples, and explanations of fundamental Java concepts, making it a useful study guide for learners. It also includes practical instructions for using Java development tools like NetBeans.

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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views31 pages

Prgramming With Java Notes

The document provides a series of sample short questions and answers related to Java programming, covering topics such as Object Oriented Programming (OOP), Java components (JVM, JRE, JDK), classes and objects, control statements, and key concepts like inheritance, polymorphism, encapsulation, and abstraction. Each section includes definitions, examples, and explanations of fundamental Java concepts, making it a useful study guide for learners. It also includes practical instructions for using Java development tools like NetBeans.

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 PDF, TXT or read online on Scribd
You are on page 1/ 31

✅ Chapter 1 – PART-II SAMPLE SHORT QUESTIONS

(Simplified, As Per Textbook)


1. What is OOP (Object Oriented Programming)?
OOP is a method of programming where we use
objects to represent real things. It includes concepts
like inheritance, encapsulation, polymorphism, and
abstraction.
2. When was Java introduced?
Java was introduced in 1995.
3. Who made Java?
Java was created by James Gosling at Sun
Microsystems.
4. What do you know about the security of Java?

5. What is JVM (Java Virtual Machine)?


JVM is a program that runs Java programs. It loads,
verifies, and runs Java bytecode on any device.
6. What are program execution phases?
o Writing code using an editor

o Compiling the code to get bytecode (javac)

o Running the bytecode using JVM

7. What is Byte Code?


Bytecode is platform-independent code created
after compiling Java code. It can run on any system
using JVM.
8. What is JRE (Java Runtime Environment)?
JRE gives the environment needed to run Java
programs. It includes JVM and some libraries.
9. State components of JRE?
o JVM

o Java Libraries

o Supporting files for execution

10. What is JDK (Java Development Kit)?


JDK is a software package used to create and run
Java programs. It contains JRE and tools like
compiler, debugger, etc.
11. What is IDE (Integrated Development
Environment)?
An IDE is a software that combines tools like editor,
compiler, and debugger in one place to help you
write and test Java programs. (e.g., NetBeans)
12. How to create a new project in NetBeans (IDE)?
o Open NetBeans

o Click File > New Project

o Choose Java with Maven → Java Application

o Click Next, give project name → Click Finish

13. How to add Class or Package in Java program?


o Right-click Source Package

o Choose New → Java Class or Java Package

o Enter name → Click Finish


14. How to build a project in Java?
In NetBeans, click Build Project from the toolbar or
right-click on project → Build.
15. What is the structure of a Java program?
A Java program includes:
o Documentation

o Package declaration

o Import statements

o Interface section (optional)

o Class definition

o Variables

o Main method

o Other methods

16. What is package?


A package is a way to organize classes into folders or
directories. It helps manage large programs.
17. What is .class?
.class is the compiled file of a Java program that
contains bytecode for the JVM to run.
18. Define class name?
The class name is the name given to a class using the
class keyword. It usually starts with a capital letter.
19. What are data members?
Data members are the variables declared in a class.
They store the data or values of an object.
20. Define methods?
Methods are functions inside a class that define the
behavior or actions of an object.
21. What are comments?
Comments are notes written in code to explain it.
Java ignores comments when running the program.
22. Define single line comment?
A comment that starts with //. It is used to write a
note in one line.
Example: // This is a single line comment
23. Define multiline comment?
A comment that spans multiple lines. It starts with /*
and ends with */.
Example:
java
CopyEdit
/* This is
a multiline comment */
24. Define documentation?
It's a special comment for writing documentation. It
starts with /** and ends with */.
Example:
java
CopyEdit
/** This is a documentation comment */
25. What is machine language?
Machine language is the binary code (0s and 1s) that
a computer understands and runs directly.
✅ Chapter 2 – PART-II SAMPLE SHORT QUESTIONS
(From Exercise)
(CIT-212: Variables and Operators)

1. What is the syntax of variable?


A variable is declared using the format:
data_type variable_name = value;
Example: int a = 30;
2. Define and explain identifier?
An identifier is the name given to variables, classes,
methods, etc. It must begin with a letter, underscore
_, or dollar sign $ and cannot be a Java keyword.
3. What are comments?
Comments are notes in code that explain the logic
and are ignored by the compiler. They help make
code readable.
4. Define literals?
Literals are fixed values used in Java programs.
Example: numbers (5), characters ('A'), strings
("Java").
5. Define keyword?
Keywords are reserved words in Java that have
predefined meanings. Example: int, class, if, for.
6. Define operator and its types?
Operators are symbols that perform operations on
variables.
Types: Arithmetic, Relational, Logical, Assignment,
Unary, Bitwise, Ternary.
7. Define arithmetic operator?
Arithmetic operators perform basic math
operations.
Example: +, -, *, /, %
8. Define relational operator?
Relational operators compare values and return a
boolean result.
Example: ==, !=, >, <, >=, <=
9. Define logical operator?
Logical operators check multiple conditions.
Example: && (AND), || (OR), ! (NOT)
10. Define bitwise operator?
Bitwise operators perform operations on bits of
numbers.
Example: &, |, ^, ~, <<, >>
11. Define increment operator?
++ increases a variable’s value by 1.
Example: a++ or ++a
12. Define decrement operator?
-- decreases a variable’s value by 1.
Example: a-- or --a
13. What is prefix operator?
The operator comes before the variable.
Example: ++a (increment first, then use)
14. What is postfix operator?
The operator comes after the variable.
Example: a++ (use first, then increment)
15. What is prefix decrement operator?
The value is decreased first, then used.
Example: --a
16. Define postfix decrement operator?
The value is used first, then decreased.
Example: a--
17. Define assignment operator?
Assignment operators assign values to variables.
Example: =, +=, -=, *=, /=
18. Define unary operator?
Unary operators work with a single operand.
Example: +, -, ++, --, !
19. What is ternary operator?
A conditional operator that works like if-else.
Syntax: condition ? value_if_true : value_if_false;
20. What is modulus operator?
% gives the remainder after division.
Example: 10 % 3 = 1
✅ Chapter 3 – PART-II SAMPLE SHORT QUESTIONS
(From Exercise | Based on CIT-212 Java Textbook)
Topic: Classes and Objects

1. What is an object?
An object is a real-world entity created from a class.
It has properties (variables) and behaviors
(methods). For example, a Student object can have
name, roll number, etc.
2. Create an object.
Main myObj = new Main();
This creates an object myObj from the class Main.

3. What is class?
A class is a template or blueprint used to create
objects. It contains data members and methods.
Example: class Student { }
4. Create a class.
public class Student {
String name;
int rollNo;
}
5. What is a method?
A method is a function inside a class that performs
actions. It defines the behavior of an object.
6. Create a method.
public void showMessage() {
System.out.println("Hello!");
}
7. Define Method Overloading?
Method overloading means defining multiple
methods with the same name but with different
parameters in a class.
8. Write Method Overloading Benefits?
o It makes the program easier to understand and

write.
o It allows different tasks with the same method

name, depending on parameters.


9. Write Uses of Method Overriding?
o To provide a specific implementation of a

method in a subclass.
o Used in runtime polymorphism.

10. Define Constructor?


A constructor is a special method that is
automatically called when an object is created. It is
used to initialize objects.
11. Define access modifiers?
Access modifiers define the visibility or accessibility
of classes, methods, and variables. Java supports
private, protected, public, and default.
12. Define Private Access Modifier?
The private modifier means that the member is
accessible only within the same class.
13. Define Protected Access Modifier?
The protected modifier means the member is
accessible within the same package and subclasses
(even in other packages).
14. What is default constructor?
A constructor with no parameters is called a default
constructor. If no constructor is defined, Java
automatically provides a default one.
15. What are Parameterized Constructors?
Parameterized constructors have parameters. They
are used to initialize objects with specific values.
✅ Chapter 4 – PART-II SAMPLE SHORT QUESTIONS
(From Exercise | Based on CIT-212 Java Textbook)
Topic: Control Statements

1. What are iteration / Looping Statements?


Iteration or looping statements are used to repeat a
block of code multiple times. Examples: for, while,
do-while.
2. What are jump / Branching Statements?
These are used to change the normal flow of a
program. Examples: break, continue, return.
3. What is Infinite while loop in Java?
A while loop that has a condition which always
remains true and never ends is called an infinite
loop.
Example:
while(true) {
// endless code
}
4. What is the importance of Java for-each loop?
The for-each loop is used to iterate over arrays or
collections easily, without using an index or counter.
5. Is there a way to access an iteration-counter in
Java's for-each loop?
No, the enhanced for-each loop does not provide
access to a loop counter directly.
6. Advantages of declaring loop variable as final in
enhanced for-loop.
Declaring loop variable as final ensures it can’t be
modified inside the loop, improving safety and
clarity.
7. Does returning a value from the loop body break
the loop?
Yes, if a return statement is used, it immediately
exits the method, thus breaking the loop as well.
8. What happens when we forget break in switch
case?
If break is forgotten, all cases below the matched
one are executed (this is called “fall through”).
9. How will you exit anticipatedly from a loop?
Use the break statement to exit a loop early based
on a condition.
10. What are Flow Charts?
Flow charts are diagrams that show the steps of a
program in a visual format, using arrows and shapes.
11. What is the syntax of nested if statement?
if(condition1) {
if(condition2) {
// statements
}
}
12. What can break statement do in switch case?
It exits the switch block after a specific case is
executed to avoid fall-through.
13. Draw the Flow Chart of While Loop.
[Start] → [Condition?] → Yes → [Body] → [Back to
Condition]
→ No → [End]
(A simple loop with a decision diamond pointing to loop
body and looping back)
14. Difference between dead code and
unreachable code in Java.
 Dead Code: Code that can run but never affects the
program output.
 Unreachable Code: Code that the compiler detects
as never going to run at all (e.g., after a return).
15. Which is considered as a selection statement?
Statements like if, if-else, and switch are selection
statements because they select blocks to execute
based on conditions.
16. What is the difference between while and do-
while loop?
 while: checks condition before running the loop
body.
 do-while: checks condition after executing the body
once.
17. What are cases in a switch?
case labels are used to match specific values in a
switch block and run related code.
18. Can we write switch case using a double
variable?
No, switch in Java does not support double data
type.
19. What is the output of Java program with IF
statement?
if(1) {
System.out.println("OK");
}

❌ Error. The condition must be a boolean, not an


integer.
20. What is the output of the Java program with IF-
ELSE-IF statements?
int marks = 55;
if(marks >= 80)
System.out.println("DISTINCTION");
else if(marks >= 35)
System.out.println("PASS");
else
System.out.println("FAIL");

Output: PASS
✅ Chapter 5 – PART-II SAMPLE SHORT QUESTIONS

(Topic: Inheritance, Polymorphism, Encapsulation,


Abstraction)

1. How is Inheritance implemented in Java?


By using the extends keyword. A subclass inherits
properties and methods from a parent class.
2. Are static members inherited to subclasses?
Yes, static members are inherited, but they are
accessed using the class name.
3. What happens if the parent and child class have a
field with the same identifier?
The child class hides the parent’s field. Use super to
access the parent's field.
4. Are constructors and initializers also inherited to
subclasses?
No, constructors are not inherited, but the child class
can call them using super().
5. How do you restrict a member of a class from being
inherited?
By declaring it private.
6. How do you implement multiple inheritance in
Java?
Java does not allow multiple inheritance with classes
but allows it with interfaces.
7. How many types of inheritance are there?
Single, Multilevel, Hierarchical inheritance. (Multiple
inheritance through interfaces)
8. Can a class extend itself in Java?
❌ No, a class cannot extend itself.
9. Can we reduce the visibility of the inherited or
overridden method?
❌ No, visibility can only stay the same or increase.
10. How do you override a private method in Java?
❌ Private methods cannot be overridden because
they are not inherited.
11. Why use Inheritance?
To achieve code reusability and make programs
easier to maintain and extend.
12. What is the General format of Inheritance?
java
CopyEdit
class ChildClass extends ParentClass { }
13. Why is Multiple Inheritance not supported in
Java?
To avoid ambiguity (diamond problem).
14. What is Polymorphism?
One interface, multiple forms. Methods behave
differently based on the object.
15. What is Compile-time Polymorphism (Static
Polymorphism)?
Achieved by method overloading during
compilation.
16. What is Run-time Polymorphism (Dynamic
Polymorphism)?
Achieved by method overriding during runtime.
17. What is Method Overloading?
Same method name but different parameters in the
same class.
18. What is Method Overriding?
A subclass provides its own version of a method
defined in the parent class.
19. What are three ways to overload a method?
o Change the number of parameters

o Change data types of parameters

o Change the sequence of parameters

20. What is an invalid case of method overloading?


Changing only the return type is invalid.
21. What is Type Promotion?
Automatically converting smaller data types to larger
types during operations.
22. What is the advantage of Method Overriding?
Allows a subclass to provide a specific
implementation for a method.
23. What are Access Modifiers?
Keywords that define the visibility of classes,
methods, or variables: private, default, protected,
public.
24. What is the advantage of Polymorphism?
It increases code flexibility and reusability.
25. What is Information Hiding?
Hiding internal class details and exposing only
necessary parts.
26. Real-life Example of Information Hiding?
Using a TV remote without knowing its internal
working.
27. What is Encapsulation?
Combining data and methods into one unit (class)
and restricting access.
28. How can we achieve encapsulation in Java?
By declaring variables private and using getter and
setter methods.
29. What is Abstraction?
Showing only important features and hiding
unnecessary details.
30. What are Abstract Classes?
Classes that cannot be instantiated and may contain
abstract methods. Declared using the abstract
keyword.
✅ Chapter 6 – PART-II SAMPLE SHORT QUESTIONS
(Solved According to Textbook)

(Topic: Interfaces)

1. Define an Interface.
An interface is a collection of abstract methods. It is
a blueprint for classes to implement.
2. How to declare an Interface?
Use the interface keyword.
Example:
java
CopyEdit
interface MyInterface {
void show();
}
3. What is Abstract Class?
An abstract class is a class that cannot be
instantiated and may contain abstract methods
(without body) and normal methods (with body).
4. Write any 2 uses of Interface.
o Achieve abstraction.

o Achieve multiple inheritance.


5. What is the keyword used to extend Interface?
The keyword is extends.
6. How to extend multiple Interfaces?
An interface can extend multiple interfaces by using
commas.
Example:
java
CopyEdit
interface A extends B, C { }
7. Write syntax to declare Interface.
java
CopyEdit
interface InterfaceName {
// method declarations
}
8. What specifier is used for Interface?
Interfaces are public by default (or package-private if
no specifier is used). Methods are public and
abstract.
9. Write short example to implement an Interface.
java
CopyEdit
interface MyInterface {
void show();
}

class MyClass implements MyInterface {


public void show() {
System.out.println("Hello");
}
}
31. What is the keyword used to inherit an
Interface?
To inherit (implement) an interface into a class, we
use the implements keyword.
✅ Chapter 7 – PART-II SAMPLE SHORT QUESTIONS
(Solved According to Textbook)

(Topic: Generics in Java)

1. Why do we use Generics in Java?


Generics provide type safety and reusability of code.
They allow classes, interfaces, and methods to
operate on different data types without losing type
information.
2. Are Generics important in Java?
✅ Yes, Generics are important to avoid type
casting, catch errors at compile time, and improve
code clarity.
3. When did Java add Generics?
Java added Generics in Java SE 5.0 (also called JDK
1.5).
4. What is a Generic type?
A generic type is a class or interface that works with
any data type.
Example:
java
CopyEdit
class Box<T> {
T value;
}
5. Can we use Generics with Array in Java?
Generics and arrays do not work directly together
because of type erasure.
However, arrays of generic types can be created
carefully using warnings or suppression.
6. Define classes in Generics.
A generic class uses a type parameter to handle
multiple data types.
Example:
java
CopyEdit
class MyClass<T> {
T obj;
}
7. What are interfaces in Generics?
A generic interface defines methods using type
parameters, allowing different implementations for
different types.
Example:
java
CopyEdit
interface MyInterface<T> {
void method(T t);
}
8. Define methods in Generics.
A generic method is a method that declares its own
type parameters.
Example:
java
CopyEdit
<T> void display(T data) {
System.out.println(data);
}
9. Define generic constructor.
A generic constructor uses a type parameter. It can
accept arguments of any type.
Example:
java
CopyEdit
class Demo {
<T> Demo(T data) {
System.out.println(data);
}
}
10. Write about bounded parameters of Generics.
Bounded parameters restrict the types that can be
used.
Example:
java
CopyEdit
<T extends Number> void method(T num) { }
(Here T must be a Number or its subclass.)
✅ Chapter 8 – PART-II SAMPLE SHORT QUESTIONS
(Solved According to Textbook)

(Topic: Exception Handling in Java)

1. What are Java exceptions?


Exceptions are problems or errors that occur during
program execution, which disrupt the normal flow
of the program.
2. How to handle exceptions in Java?
Use try-catch blocks:
o Code that may cause an exception is placed in

the try block.


o Handling code is placed inside the catch block.

3. What is the difference between exceptions and


errors in Java?
o Exceptions are problems that can often be

recovered during program execution.


o Errors are serious issues that usually cannot be

handled (e.g., memory issues).


4. What are checked and unchecked exceptions in
Java?
o Checked exceptions must be handled using try-

catch or declared using throws.


o Unchecked exceptions occur at runtime and are
not required to be handled.
5. Why is the term finally used?
The finally block always executes after try-catch,
whether an exception occurs or not. It is used for
clean-up activities like closing files.
6. Give some examples of unchecked exceptions.
o ArithmeticException

o NullPointerException

o ArrayIndexOutOfBoundsException

7. Give some examples of checked exceptions.


o IOException

o SQLException

o FileNotFoundException

8. Can we throw an exception manually?


✅ Yes, we can throw an exception manually using
the throw keyword.
Example:
java
CopyEdit
throw new ArithmeticException("Divide by zero");
9. What is Java multicatch exception?
In Java, multi-catch allows catching multiple
exception types in a single catch block using the |
(pipe) symbol.
Example:
java
CopyEdit
catch(IOException | SQLException ex) { }
10. What is meant by print exception in Java?
Printing an exception means displaying exception
details using methods like printStackTrace() or
getMessage().

You might also like