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

Java Control Structures and OOP Principles

The document provides an overview of various programming concepts, including control statements like switch-case and if-else, iteration through loops, object-oriented programming principles, and Java-specific features such as constructors and data types. It explains the syntax and functionality of different programming constructs, including the use of operators, functions, and access specifiers. Additionally, it covers the Java Virtual Machine (JVM), Java Runtime Environment (JRE), and the concept of bytecode, highlighting the importance of these components in Java programming.

Uploaded by

dkoley1982
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)
3 views12 pages

Java Control Structures and OOP Principles

The document provides an overview of various programming concepts, including control statements like switch-case and if-else, iteration through loops, object-oriented programming principles, and Java-specific features such as constructors and data types. It explains the syntax and functionality of different programming constructs, including the use of operators, functions, and access specifiers. Additionally, it covers the Java Virtual Machine (JVM), Java Runtime Environment (JRE), and the concept of bytecode, highlighting the importance of these components in Java programming.

Uploaded by

dkoley1982
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

Switch case statement/ Menu driven/ User’s choice :-

This is a branching control statement where the end of each case acts as an exit from the
switch statements. In this case, a number of statements are put together with different cases.
This switch statement works as a jumping statement, where the control is transferred to a
specific case as per the given switch value. Each case ends with a break statement which acts
as a terminator and the control comes out of the block. It also includes default statement. The
default statement is executed only when the switch value does not match with a case. It does
not require any usage of break as it is the last statement of the switch case structure.

Syntax: -

switch(ch)​
{​
case 1:​
statement;​
break;​
case 2:​
statement;​
break;​
default:​
statement;​
}

Fall through:-

In a program where multiple cases are available, the control needs to be executed for a specific
block for a given switch’s [Link] the circumstances, if the break statement is not applied at
the end of a case then the control enters into the next case statement for execution. This
unusual execution of more than one case at a time is termed as fall through.

If else

The if-else statement is a basic conditional control structure that allows you to execute one
block of code if a condition is true and another block if the condition is false.

Syntax:-

if (condition) ​
{ ​
// Code to execute if the condition is true​
} ​
else ​
{ ​
// Code to execute if the condition is false ​
}

If else-if else

An if-else-if-else statement is used to execute specific blocks of code based on whether certain
conditions are true. It is a conditional control structure that allows for multiple branching
decisions.

Syntax:-

if (condition1) ​
{ ​
// Code to execute if condition1 is true ​
} else if (condition2) ​
{ ​
// Code to execute if condition2 is true ​
} else if (condition3) ​
{ ​
// Code to execute if condition3 is true ​
} else ​
{ ​
// Code to execute if none of the above conditions are true​
}

Iteration through loops

Based upon the nature of iterations the loops are of two types:-
i) Fixed iteration:- for loop
ii) Variable iteration:- while loop and do-while loop

i) for loop:- A for loop is used to repeatedly execute a block of code for a specific number of
times. Its syntax consists of three parts: initialization, condition, and update. It is an
entry-controlled loop.

Syntax:-
for (initialization; condition; update) ​
{ ​
// Code to be executed ​
}

Initialization:- A variable is initialized before the loop starts. This happens only once.
Condition:- The loop runs as long as this condition evaluates to true.
Update:- The value of the loop control variable is updated after each iteration.

ii) [a] while loop:- The while loop in Java is used to repeatedly execute a block of code as
long as the specified condition evaluates to true. It is an entry-controlled loop, meaning the
condition is checked before the loop body is executed.

Syntax:-
while (condition) ​
{​
// Code to be executed ​
}

KEY POINTS: -
i) The condition must return a boolean value (true or false).
ii) The loop body will not execute if the condition is false initially.
iii) The loop will continue to execute as long as the condition is true.

[b] do-while loop:- The do-while loop in Java is a type of loop that executes a block of code
at least once and then repeatedly executes it as long as the specified condition evaluates to
true. It is an exit-controlled loop, meaning the condition is checked after the loop body is
executed.

Syntax:-
do ​
{ ​
// Code to be executed ​
} ​
while (condition);

KEY POINTS: -
i) The loop body executes at least once, even if the condition is false initially.
ii) The condition is evaluated after the loop body.
iii) Use it when you need the loop to run at least once, regardless of the condition.
SHORT QUESTION: -

Q) Define all the four principles of OOPs.


→ The four principles of OOPs are:-
a)​ Encapsulation:- Encapsulation is the technique of binding both data and functions
together and it acts as a single unit, to keep them safe from any unauthorised access.
b)​ Abstraction:- Abstraction refers to the act of representing essential features without
including the background details or explanations.
c)​ Inheritance:- Inheritance is the capability of one class of things to inherit properties of
another class.
d)​ Polymorphism:- Polymorphism is the ability for a message or data to be processed in
more than one form.

Q) Name two types of Java programs


→ i) Stand-alone program
ii) Java Applets

Q) Name a package that is invoked by default.


→ [Link]

Q) Define Bytecode.
→ Java compiler converts source code to intermediate [Link] code helps generate machine
[Link] code is independent of the machine on which the program runs. It makes a java
program highly portable. Thus,this intermediate code is known as bytecode.

Q) What is the use of the keyword ‘import’?


→ The keyword import is used to include a package in java programming.

Q) State the package that contains the classes (i) Buffered Reader (ii) Scanner
→ (i) [Link] package
(ii) [Link] package

Q) Why is an object called an instance of a class?


→ Since an object possesses instance variables and member methods defined within the class.
Hence an object is called the instance of a class.

Q) What is a constructor?
→ Constructor is a special type of method which initialises instance variable at the time of object
Creation.
Q) Write down the characteristics of constructors.
→ i) class name and constructor name should be the same.
ii) If the programmer doesn't supply a constructor the java compiler supplies a default
constructor.
iii) It does not have a return value, not even void.
iv) A program can have more than one constructor, this is called constructor overloading.
v) A constructor can accept value from the user, this is called a parameterised constructor.

Q) Define i) Default constructor (ii) Parameterised constructor


→ i) Default Constructor:- A default constructor is a constructor provided automatically by the
compiler if no constructors are defined in the class. It initializes the object with default values.
ii) Parameterized Constructor:- A parameterized constructor is a constructor that accepts
arguments (parameters) to initialize an object with specific values. This allows us to provide
custom initialization for each object at the time of its creation.

Q) When a constructor is called?


→ Constructor is invoked at the time of creating objects of the class.

Q) Define formal and actual parameters.


→ Formal parameters:- Formal parameters are the parameters that appear in function
definition. They are also called parameters.
Actual Parameters:- Actual parameters are the parameters that appear in function call
statement. They are also called arguments.

Q) Define call by value or pass by value.


→ Pass by value is the process of passing a copy of the actual parameters to the formal
parameters. Any change made in formal parameters does not reflect on the actual parameter.

Q) Define pass by reference / address


→ Pass by address is the process of passing the reference of the actual parameters to the
formal parameters in such a way that any change made in the formal parameter will be reflected
in the actual one.

Q) What is function overloading?


→ Function overloading is a concept in programming where multiple functions in the same class
can have the same name, but they differ in the number of parameters and/or types of
parameters

Q) What is function/method prototype?


→ Function prototype refers to the first line of function definition. It tells the program about the
type of function returned by the method and the types and number of arguments.
Q) What is function signature?
→ It is a part of function prototype which refers to the number and types of arguments of the
function.

Q) What are the uses of functions in a program?


→ i) It decreases the length of the program.
ii) It takes less memory space for the storage of program.

Q) What is the use of the keyword ‘void’?


→ The keyword void signifies that the function does not return any value to main() function.

Q) What are pure and impure functions?


→ Pure function:- The function which has a return value and which is not a void type. The
function can return only one value at a time.
Impure function:- The function which has no return value and is void type is called impure
function.

Q) Define static and non-static functions.


→ Static functions:- The functions which are called using their class’ name outside the class or
directly without using their class name inside their own class in another method. They are also
known as class methods and use the keyword static in their function prototype.
Syntax:-
<class_name>.<function_name>(parameters)

Non-static functions:- The functions which are called by using object are called non static
functions. They are also called instance methods.
Syntax:-
class_name objectname=new Class_name(); // creating object​
<object name>.<function name>(); // calling the non-static function using objects

Q) What are the two ways of calling a function?


→ i) Pass by value (ii) Pass by reference/address

Q) What is package?
→ Package is a set of various similar type of classes. Example:- [Link].*, [Link].*, [Link]
etc.

Q) What is the use of * (asterisk) while importing a package?


→ The * (asterisk) is used while importing a package to import all the classes and interfaces
present in that package into the program. This is known as wildcard import.
Q) Why is a class called the ‘object factory’?
→ A class is used to produce various objects containing common attributes and behaviour.
Hence, it is known as an object factory.

Q) Why is a class called composite data type?


→ A class is a tool to create user-defined datatype. The class name becomes the datatype for
the instance used in the program. It is such a datatype that includes various predefined
datatypes within it. Hence, a class is said to be a composite datatype.

Q) Define type conversion. How is implicit conversion different from explicit conversion?
→ In a mixed expression, the result must be obtained in a specific data type. For this purpose
needs to be converted into the required type. This is known as type conversion.

In an implicit type conversion the result of the mixed expression is obtained in the higher
made data type of the variables in the expression without any intervention of the user.
Ex:-
int x = 10; ​
double y = x; // int to double (automatic conversion)

In case of explicit type conversion, the data type is converted to another type as per the
user’s choice and requirements.
Ex:-
double a = 9.78; ​
int b = (int) a; // double to int (manual conversion)

Q) What are primitive data types?


→ The fundamental data types offered by java are called primitive data types.
Ex:- byte(1 byte), short(2 bytes), int(4 bytes), long(8 bytes), float (4 bytes), double(8 byte) ,
char(2 byte) and boolean(1 bit).

Q) What are reference datatypes?


→ The datatypes that are constructed from primitive datatypes are called reference datatypes.
Ex:- array, classes etc.

Q) What is the use of final keyword?


→ The keyword final is used to make a variable constant. The value will not change during
program execution.
Q) What is JVM?
→ The Java Virtual Machine (JVM) is a key component of the Java programming platform that
provides an environment to run Java programs. It acts as an interpreter between Java bytecode
(a platform-independent code) and the machine-specific code of the operating system.

Q) What is JRE?
→ The Java Runtime Environment (JRE) is a software layer that provides the necessary
environment to run Java applications. It is part of the Java platform and contains everything
required to execute Java programs, including the Java Virtual Machine (JVM), libraries, and
other components.

Q) What is native executable code?


→ Native executable code refers to machine-specific code that the computer's processor can
directly execute. It is the final output after Java's intermediate bytecode has been translated into
platform-specific instructions by the Java Virtual Machine (JVM).

Q) What is Object Oriented Programming?


→ Object-Oriented Programming (OOP) is a programming paradigm that is based on the
concept of objects and classes. It focuses on organizing code into reusable components called
objects, which represent real-world entities, and using classes as blueprints for creating those
objects.

Q) What is the return statement used for?


→ The return statement in Java is used to exit from a method and optionally send a value
back to the calling method. It serves two main purposes:

1.​ To terminate the execution of a method.


2.​ To return a value (if applicable) to the method's caller.

Q) What is the use of break statement?

→ The break statement is used to terminate a loop or a switch statement and transfer the
control to the next statement following the loop or switch block. It is typically used to exit the
loop early as soon as certain condition is met.

Q) What is the use of continue statement?


→ The continue statement is used to skip the current iteration of a loop and move on to the
next iteration. When the continue statement is encountered inside a loop, the remaining code in
the current iteration is skipped, and the loop proceeds with the next iteration.

Q) What are operators?


→ Operators are special symbols or keywords used to perform operations on variables and
values. Operators allow you to manipulate data and perform various computations or logical
operations.
Types of Operator:-

1. Arithmetic Operators:- These operators are used to perform basic arithmetic


operations.

●​ +: Addition
●​ -: Subtraction
●​ *: Multiplication
●​ /: Division
●​ %: Modulus (remainder)

2. Relational (Comparison) Operators:- These operators are used to compare two


values and return a boolean result (true or false).

●​ ==: Equal to
●​ !=: Not equal to
●​ >: Greater than
●​ <: Less than
●​ >=: Greater than or equal to
●​ <=: Less than or equal to

3. Logical Operators:- These operators are used to perform logical operations, typically for
boolean values.

●​ &&: Logical AND (both must be true)


●​ ||: Logical OR (either must be true)
●​ !: Logical NOT (inverts a boolean value)

4. Assignment Operators:- These operators are used to assign values to variables.

●​ =: Simple assignment
●​ +=: Add and assign
●​ -=: Subtract and assign
●​ *=: Multiply and assign
●​ /=: Divide and assign
●​ %=: Modulus and assign
5. Increment and Decrement Operators:- These are used to increase or decrease the
value of a variable by 1.

●​ ++: Increment (increases the value by 1)


○​ Postfix (i++): Increments after the value is used.
○​ Prefix (++i): Increments before the value is used.
●​ --: Decrement (decreases the value by 1)
○​ Postfix (i--): Decrements after the value is used.
○​ Prefix (--i): Decrements before the value is used.

6. Ternary Operator:

The ternary operator is a shorthand for an if-else statement. It takes three operands:

●​ condition ? expression1 : expression2: If the condition is true,


expression1 is executed, otherwise expression2 is executed.

Example:-

int x = 10, y = 20; ​


int max = (x > y) ? x : y; // If x > y, max = x; otherwise max = y ​
[Link](max); // 20

Q) What is the new operator?


→ The new operator is used to create new objects of classes. It dynamically allocates memory
for objects and initializes them by calling the class constructor. The new operator is essential
because it allows to create objects from classes.

Output Statements:-

[Link]();​
[Link]();

●​ System: A predefined class in the [Link] package.


●​ out: A static member of the System class. It is an instance of PrintStream used for
output.
●​ println(): A method of the PrintStream class that prints the given message
followed by a newline.
●​ print(): A method of the PrintStream class that prints the given message in the
same line.
Q) What are access specifiers?

→ Access specifiers (also called access modifiers) are keywords used to control the visibility
and accessibility of classes, methods, and variables from other classes.

public: Accessible anywhere.

private: Accessible only within the same class.

protected: Accessible within the same package and by subclasses.

Default (package-private): Accessible only within the same package.

Q) What is the new operator?

→ The new operator is used to create new objects. It allocates memory for an object and calls
the constructor to initialize it.

Q) What is (.)dot operator?

→ The dot (.) operator in Java is used to access members of a class or object. It helps in
navigating through objects, calling methods, and accessing variables.

Q) Difference between ternary operator and if-else statement

→ The difference between ternary operator (?:) & if-else statement are:-

i) In comparison with if-else statement, the conditional operator (?:) offers a more concise, clean
and compact code but it is less obvious as compared to if.

ii) The ternary operator produces an expression and hence only a single value can be assigned
or incorporated into a larger expression but if else more flexible, it can contain multiple
statements, expressions and assignment.

iii) Conditional operator used in nested looks complicated and conceals the purpose of code.

Q) What is JDK?

→ JDK (Java Development Kit) is a software development environment used for developing
Java applications. It is one of the core tools in Java programming and contains everything
needed to write, compile, and run Java programs. It includes Java Compiler, Java Runtime
Environment, Java Debugger, Java Documentation Tool and Java Archive Tool.

You might also like