[Go to site: main page, start]

0% found this document useful (0 votes)
9 views17 pages

Java Programming Concepts Explained

The document compares Procedure Oriented Programming and Object Oriented Programming, highlighting key differences such as structure, security, and examples. It also explains various data types in Java, unary, logical, bitwise, relational, arithmetic, conditional, and assignment operators, along with decision-making constructs and loops. Additionally, it covers the use of arrays and methods in Java with examples.

Uploaded by

kalabilla015
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)
9 views17 pages

Java Programming Concepts Explained

The document compares Procedure Oriented Programming and Object Oriented Programming, highlighting key differences such as structure, security, and examples. It also explains various data types in Java, unary, logical, bitwise, relational, arithmetic, conditional, and assignment operators, along with decision-making constructs and loops. Additionally, it covers the use of arrays and methods in Java with examples.

Uploaded by

kalabilla015
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

Q1: Difference between Procedure oriented and Object Oriented Programming

approach:
Solution:
S.N. Procedure Oriented Programming Object Oriented Programming

1 Program is divided into functions. program is divided into objects

2 Follows a top-down approach. Follows bottom up approach.

3 Less secure as no data hiding. More secure because of data hiding

4 No overloading is possible. Overloading is possible

5 No concept of data hiding and concept of data hiding and inheritance


inheritance

6 Based on the unreal world. Based on the real world.

7 Examples: C, FORTRAN, Pascal, Examples: C++, Java, Python, C#, etc.


Basic, etc.

1
Q2: Explain various data types in Java.
Solution:
There are 8 Primitive data types in Java – Boolean, char, byte, int, short, long, float, and
double.
S.N. Data type Size Range Example
1 Byte 1 byte = 8 bits -128 to +127 byte a;
2 Short 2 byte = 16 bits -215 to 215-1 short a;
3 Int 4 byte = 32 bits -231 to 231-1 int a;
4 Long 8 byte = 64 bits -263 to 263-1 long a;
5 Char 2 byte = 16 bits 0 to 255 char a;
6 Float 4 byte = 32 bits upto 7 decimal digits float a;
7 Double 8 byte = 32 bits upto 16 decimal digits double s;
8 Boolean 1 bit True, false boolean a;

String is made up of characters

Q3: Explain Unary operators in Java


Solution:
Unary Operators:
++ : Increment operator, used for incrementing the value by 1. There are two varieties of
increment operators.
Post-Increment: Value is first used for computing the result and then incremented.
Pre-Increment: Value is incremented first, and then the result is computed.
– – : Decrement operator, used for decrementing the value by 1. There are two varieties of
decrement operators.
Post-decrement: Value is first used for computing the result and then decremented.
Pre-Decrement: The value is decremented first, and then the result is computed.

2
Q4: Explain Logical operators in Java.
Solution:
Logical operators in java are operators that help us combine multiple condition.
AND Operator (&&) :
If (Cond1 && Cond2)
{
//statements
}
else
{
//statements
}

Logical OR operators (||):


If (Cond1 || Cond2):
{
//statements
}
else
{
//statements
}

NOT Operator ( ! ) – !(a<b) [returns false if a is smaller than b]

&&, Logical AND: returns true when both conditions are true.
||, Logical OR: returns true if at least one condition is true.
!, Logical NOT: returns true when a condition is false and vice-versa
boolean x = true;
boolean y = false;
[Link]("x && y: " + (x && y));
[Link]("x || y: " + (x || y));
[Link]("!x: " + (!x));
Output
x && y: false
x || y: true
!x: false

3
Q5: Explain bitwise operators in Java.
Solution:
These operators are used to perform the manipulation of individual bits of a number.
&, Bitwise AND operator: returns bit by bit AND of input values.
|, Bitwise OR operator: returns bit by bit OR of input values.
^, Bitwise XOR operator: returns bit-by-bit XOR of input values.
~, Bitwise Complement Operator: This is a unary operator which returns the one’s
complement representation of the input value, i.e., with all bits inverted.
<<, Left shift operator: shifts the bits of the number to the left and fills 0 on voids left as a
result. Similar effect as multiplying the number with some power of two.
>>, Signed Right shift operator: shifts the bits of the number to the right and fills 0 on voids
left as a result.

int d = 10;
int e = 12;
[Link]("d & e: " + (d & e));
[Link]("d | e: " + (d | e));
[Link]("d ^ e: " + (d ^ e));
[Link]("~d: " + (~d));
[Link]("d << 2: " + (d << 2));
[Link]("e >> 1: " + (e >> 1));
[Link]("e >>> 1: " + (e >>> 1));
Output
d & e: 8
d | e: 14
d ^ e: 6
~d: -11
d << 2: 40
e >> 1: 6
e >>> 1: 6

4
Q6: Explain Relational operators in Java.
Solution:
These operators are used to check for relations like equality, greater than, and less than. They
return boolean results after the comparison and are used in loop statements and if-else
statements.
Some of the relational operators are-
==, Equal to returns true if the left-hand side is equal to the right-hand side.
!=, Not Equal to returns true if the left-hand side is not equal to the right-hand side.
<, less than: returns true if the left-hand side is less than the right-hand side.
<=, less than or equal to returns true if the left-hand side is less than or equal to the right-hand
side.
>, Greater than: returns true if the left-hand side is greater than the right-hand side.
>=, Greater than or equal to returns true if the left-hand side is greater than or equal to the
right-hand side.
Example:
int a = 10;
int b = 3;
int c = 5;
[Link]("a > b: " + (a > b));
[Link]("a < b: " + (a < b));
[Link]("a >= b: " + (a >= b));
[Link]("a <= b: " + (a <= b));
[Link]("a == c: " + (a == c));
[Link]("a != c: " + (a != c));

Output
a > b: true
a < b: false
a >= b: true
a <= b: false
a == c: false
a != c: true

5
Q7: Explain Arithmetic Operators in Java
Solution:
* : Multiplication
/ : Division
% : Modulo
+ : Addition
– : Subtraction
Example:
int a = 10;
int b = 3;
[Link]("a + b = " + (a + b));
[Link]("a - b = " + (a - b));
[Link]("a * b = " + (a * b));
[Link]("a / b = " + (a / b));
[Link]("a % b = " + (a % b));

Output
a + b = 13
a-b=7
a * b = 30
a/b=3
a%b=1

6
Q8: Explain Conditional Operators in Java
Solution:
Java ternary operator is the only conditional operator that takes three operands. It’s a one-
liner replacement for the if-then-else statement
Syntax:
variable = Expression1 ? Expression2: Expression3
Example:
num1 = 10;
num2 = 20;
res=(num1>num2) ? (num1+num2):(num1-num2)

Since num1<num2,
the second operation is performed
res = num1-num2 = -10

Q9: Explain Assignment operators in Java.


Solution:
Java assignment operators are commonly used to assign values to variables.
(+=) operator:
This operator is a compound of ‘+’ and ‘=’ operators. It adds the current value of the variable
on the left to the value on the right and then assigning the result to the operand on the left.
a += 10 means, a = a + 10
Similarly we have
a -= 10 means, a = a - 10
a *= 10 means, a = a * 10
a /= 10 means, a = a / 10
a /= 10 means, a = a / 10

7
Q10: Explain Decision Making in Java
Solution:
1. Simple if:
if (condition) {
// statement to be executed if the condition is true
}
2. if..else
if (condition) {
// statement to be executed if the condition is true
} else {
// statement of code to be executed if the condition is false
}
3. Nested if:
if (condition1) {
if (condition2) {
// statement to be executed if the condition1 is true and condition2 is true
} else {
// statement to be executed if the condition1 is true and condition2 is false
}
} else{
if (condition3) {
// statement to be executed if the condition1 is false and condition3 is true
} else {
// statement to be executed if the condition1 is false and condition3 is false
}
}
4. else if ladder:
if (condition1) {
// statement to be executed if condition1 is true
} else if (condition2) {

8
// statement to be executed if the condition1 is false and condition2 is true
} else {
// statement to be executed if the condition1 is false and condition2 is false
}

9
Q11: Explain various loops in Java
Solution:
A loop allows code to be executed repeatedly based on some condition.
3 types of loop are: while, for, do…while loop
While loop: while loop checks condition before executing the statements, and it is an Entry
Control Loop.
Syntax:
initialization
while (test condition)
{
loop statements...
// loop update statement
}
Example:
class SRGP {
public static void main (String[] args) {
int i=0;
while (i<=10)
{
[Link](i);
i++;
}
}
}
For Loop:
for statement puts the initialization, condition and increment/decrement in one line
for (initialization condition; testing condition;increment/decrement)
{
statement(s)
}

10
do while: do while loop checks condition after executing the statements, and it is an Exit
Control Loop.
Syntax:
initialization
do
{
loop statements...
// loop update statement
} while (test condition);

11
Q12: Differentiate between while and do while loop
Solution:

while do-while

Condition is checked first then statement(s) Statement(s) is executed atleast once,


is executed. then condition is checked.

It might occur statement(s) is executed zero At least once the statement(s) is


times, If condition is false. executed.

No semicolon at the end of while. Semicolon at the end of while.


while(condition) while(condition);

If there is a single statement, brackets are


Brackets are always required.
not required.

while loop is entry controlled loop. do-while loop is exit controlled loop.

while(condition) do { statement(s); }
{ statement(s); } while(condition);

12
Q13: Explain Switch…case in Java with example
Solution:
Java Switch Statements
Instead of writing many if…else statements, you can use the switch statement.
The switch statement selects one of many code blocks to be executed:
Syntax:
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
Example:
// Java program to Demonstrate Switch Case
public class SRGP {
// Main driver method
public static void main(String[] args)
{
int day = 5;
// Switch statement with int data type
switch (day) {
// Case
case 1:
[Link]("Monday");
break;
// Case

13
case 2:
[Link]("Tuesday");
break;
// Case
case 3:
[Link]("Wednesday");
break;
// Case
case 4:
[Link]("Thursday");
break;
// Case
case 5:
[Link]("Friday");
break;
// Case
case 6:
[Link]("Saturday");
break;
// Case
case 7:
[Link]("Sunday");
break;
// Default case
default:
[Link]("Invalid day");
}
}
}

14
Q14: Explain Use of Array in Java with example
Solution:
Arrays are used to store multiple values in a single variable, instead of declaring separate
variables for each value.
import [Link].*;
class MyArray
{
public static void main(String args[])
{
int value[]=new int[10];
Scanner sc=new Scanner([Link]);
[Link]("Enter 10 integers");
for(int i=0;i<10;i++)
{
value[i]=[Link]();
}
for(int i=0;i<10;i++)
{
[Link](value[i]);
}
}
}

15
Q15: Explain Use of Methods in java with example.
Solution:
A method is a block of code which only runs when it is called. You can pass data, known as
parameters, into a method.
//Program for Prime Number
import [Link].*;
class Prime
{
public static void main(String args[])
{
int n,result;
Scanner sc=new Scanner([Link]);
n=[Link]();
result=isPrime(n);
if(result==1)
{
[Link]("Number is Prime");
}
else
{
[Link]("Number is not Prime");
}
}

static int isPrime(int n)


{
int count=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
{

16
count++;
}
}
if(count==2)
{
return 1;
}
else
{
return 0;
}
}
}

17

Common questions

Powered by AI

The Switch-Case statement in Java evaluates an expression and matches its result against multiple case values, executing the corresponding code block. Unlike multiple if-else statements that evaluate conditions sequentially, a switch statement efficiently maps a single expression to various outcomes, simplifying complex decision logic and enhancing code readability. This structure is advantageous when dealing with variables showing a limited set of known discrete values, like days of the week or menu selections, where sequential boolean logic would be verbose and error-prone .

Relational operators in Java (==, !=, <, >, <=, >=) enable comparison between variables or expressions, crucially returning boolean results of true or false. Within control structures like if-else statements and loops, these boolean outcomes determine the flow of execution based on specified conditions. The boolean results provide an efficient and clear mechanism to manage flow control and ensure logical correctness in decision-making and evaluations within the code .

Bitwise operators in Java, such as &, |, ^, ~, <<, and >>, operate on the binary representations of integers, manipulating individual bits. They are crucial in tasks that require low-level data manipulation, such as graphics programming or networking. For example, the bitwise AND (&) operator performs a bitwise conjunction, the OR (|) operator combines bits, XOR (^) operator produces a value where bits are set only when corresponding bits are different, while the NOT (~) operator inverts all bits. The shift operators, << and >>, move bits to the left or right, effectively multiplying or dividing the number by two's powers, respectively. These operations allow for performance optimizations and resource-efficient data processing .

Logical operators (&&, ||, !) in Java allow for the combination and inversion of boolean expressions to form more complex conditional logic. The AND operator (&&) returns true only if both conditions are true, whereas the OR operator (||) returns true if at least one condition is true. The NOT operator (!) inverts the boolean value. These operators enable efficient decision-making processes by providing concise and expressive syntax for complex conditional statements, enhancing code readability and execution flow .

Java has eight primitive data types, each with specific size and range constraints. For example, 'byte' uses 1 byte and ranges from -128 to +127, while 'int' uses 4 bytes and ranges from -2,147,483,648 to 2,147,483,647. These constraints affect programming decisions, especially in resource-constrained environments or when interfacing with hardware, as programmers must choose data types that efficiently manage memory while fitting the required range of values .

In Java, a 'while' loop evaluates its condition before executing the loop body, suitable for cases where the number of iterations is unknown; the loop may execute zero times if the condition is initially false. Conversely, a 'do-while' loop checks its condition after executing the loop body, ensuring the loop executes at least once. Thus, 'do-while' loops are preferable for scenarios needing at least one execution regardless of the condition, such as in menus requiring user input evaluations .

In Java, arrays store multiple values of the same type in a single variable, allowing efficient data management and access through indexed positions. They are integral in scenarios involving repeated operations on multiple data items, providing fixed-size, structured storage that enables homogeneous data processing. Arrays streamline computations and data manipulation, which is vital in scenarios requiring aggregated data processing, like mathematical operations, sorting algorithms, or applications demanding frequent retrieval and updates of collections .

Unary operators in Java, such as ++ and --, modify the operand by incrementing or decrementing its value by one. Their impact on expressions differs based on their application. Pre-increment (++a) modifies the operand's value before it is used in the expression, while post-increment (a++) uses the operand’s value in the expression first and then increments it. The choice between pre and post operations can affect expression outcomes, especially in complex calculations or loops .

The ternary conditional operator in Java simplifies if-else logic into a concise expression: condition ? expression1 : expression2. It evaluates the condition; if true, expression1 is executed; otherwise, expression2 is executed. This operator is advantageous when a decision leads to simple assignments or operations, reducing verbosity, enhancing readability, and making the code more elegant, particularly in scenarios requiring inline, single-expression evaluations without full control structures .

Procedure-oriented programming divides programs into functions and follows a top-down approach, whereas object-oriented programming divides programs into objects and follows a bottom-up approach. This fundamental difference impacts security and data management, as procedural programming lacks data hiding, making it less secure. In contrast, object-oriented programming supports data hiding and inheritance, providing more secure data management by encapsulating data within objects. Additionally, object-oriented programming allows for function and operator overloading, which is not possible in procedural programming, enhancing modularity and code reusability .

You might also like