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

Java Notes

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)
8 views17 pages

Java Notes

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
You are on page 1/ 17

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;
System.out.println("x && y: " + (x && y));
System.out.println("x || y: " + (x || y));
System.out.println("!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;
System.out.println("d & e: " + (d & e));
System.out.println("d | e: " + (d | e));
System.out.println("d ^ e: " + (d ^ e));
System.out.println("~d: " + (~d));
System.out.println("d << 2: " + (d << 2));
System.out.println("e >> 1: " + (e >> 1));
System.out.println("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;
System.out.println("a > b: " + (a > b));
System.out.println("a < b: " + (a < b));
System.out.println("a >= b: " + (a >= b));
System.out.println("a <= b: " + (a <= b));
System.out.println("a == c: " + (a == c));
System.out.println("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;
System.out.println("a + b = " + (a + b));
System.out.println("a - b = " + (a - b));
System.out.println("a * b = " + (a * b));
System.out.println("a / b = " + (a / b));
System.out.println("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)
{
System.out.println(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:
System.out.println("Monday");
break;
// Case

13
case 2:
System.out.println("Tuesday");
break;
// Case
case 3:
System.out.println("Wednesday");
break;
// Case
case 4:
System.out.println("Thursday");
break;
// Case
case 5:
System.out.println("Friday");
break;
// Case
case 6:
System.out.println("Saturday");
break;
// Case
case 7:
System.out.println("Sunday");
break;
// Default case
default:
System.out.println("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 java.util.*;
class MyArray
{
public static void main(String args[])
{
int value[]=new int[10];
Scanner sc=new Scanner(System.in);
System.out.println("Enter 10 integers");
for(int i=0;i<10;i++)
{
value[i]=sc.nextInt();
}
for(int i=0;i<10;i++)
{
System.out.println(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 java.util.*;
class Prime
{
public static void main(String args[])
{
int n,result;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
result=isPrime(n);
if(result==1)
{
System.out.println("Number is Prime");
}
else
{
System.out.println("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

You might also like