0% found this document useful (0 votes)
22 views11 pages

Java T-1

The document outlines various types of arithmetic, bitwise, and relational operators in Java, detailing their functions and comparisons. It explains concepts such as operator precedence, increment and decrement operators, and the representation of negative numbers using one's and two's complement. Additionally, it includes code snippets to illustrate the application of these operators and their outputs.

Uploaded by

dheeraj72006
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)
22 views11 pages

Java T-1

The document outlines various types of arithmetic, bitwise, and relational operators in Java, detailing their functions and comparisons. It explains concepts such as operator precedence, increment and decrement operators, and the representation of negative numbers using one's and two's complement. Additionally, it includes code snippets to illustrate the application of these operators and their outputs.

Uploaded by

dheeraj72006
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

1.

Arithmetic Operators

a) List the types of arithmetic operators available in Java. [R]

The primary arithmetic operators available in Java for numeric types are:

 Addition (+)

 Subtraction (-)

 Multiplication (*)

 Division (/)

 Modulus (%) (Remainder)

Java also includes Unary Arithmetic Operators like unary plus (+), unary minus (-),
increment (++), and decrement (--).

b) Compare unary with binary operators. [U]

Feature Unary Operator Binary Operator

Number of
Works on one operand Works on two operands
Operands

Examples +, -, ++, --, ~, ! +, -, *, /, %, &, |, >, <

Used for negating, incrementing, Used for performing operations like


Function decrementing, or inverting a addition, subtraction, or comparison
single value. between two values.

c) Examine the below code to find its output. [An]

The modulus operator (%) for floating-point numbers in Java is defined as: $a \% b = a -
(b * q)$, where $q$ is the integer nearest to the exact value of $a/b$, unless $|a/b - q| =
1/2$, in which case $q$ is the even integer nearest to $a/b$. However, for the Java
double type, the remainder is calculated by the formula $a - (b \cdot n)$, where $n$ is
the mathematical integer nearest the exact value of $a/b$. If the fractional part is
exactly $0.5$, $n$ is the even integer.

In simpler terms for this problem, the sign of the result of d1 % 2 is the same as the sign
of the dividend, d1.

Java

double d1 = 10.5;
// a)

[Link](d1 % 2);

// Calculation: 10.5 - (2 * 5) = 10.5 - 10.0 = 0.5

// b)

[Link](d1 % -2);

// Calculation: 10.5 - (-2 * -5) = 10.5 - 10.0 = 0.5

// Note: The sign of the result is the same as the sign of the dividend (10.5).

d1 = -10.5;

// c)

[Link](d1 % 2);

// Calculation: -10.5 - (2 * -5) = -10.5 - (-10.0) = -0.5

// Note: The sign of the result is the same as the sign of the dividend (-10.5).

// d)

[Link](d1 % -2);

// Calculation: -10.5 - (-2 * 5) = -10.5 - (-10.0) = -0.5

// Note: The sign of the result is the same as the sign of the dividend (-10.5).

0.5

0.5

-0.5

-0.5

2. Precedence and Associativity

a) What is precedence and associativity with respect to the operators? [R]


 Precedence (or Operator Precedence) specifies the priority order in which
operators in an expression are evaluated. Operators with higher precedence are
evaluated before operators with lower precedence. For example, multiplication
(*) has higher precedence than addition (+).

 Associativity defines the direction of evaluation (either left-to-right or right-to-


left) when an expression contains two or more operators with the same level of
precedence. Most Java binary operators (like +, -, *, /) are left-to-right
associative, while assignment operators (=, +=, etc.) and unary operators are
right-to-left associative.

b) Di erentiate $a = a + 10$ and $a += 10$. [U]

Feature Simple Assignment: a=a+10 Compound Assignment: a+=10

Explicitly performs the addition and Combines the addition and


Operation
then the assignment. assignment into a single operation.

Performs an implicit cast (or


No automatic type casting. If $a$ is a
coercion) to the type of the left-
Type byte, short, or char, the result of $a +
hand operand. If $a$ is a byte, the
Casting 10$ is an int, requiring an explicit cast
result is automatically cast back to
to assign it back to $a$.
byte.

More concise and generally


Verbosity More verbose.
preferred for readability.

Generally similar, but compound


assignment may be slightly faster in
E iciency
some low-level implementations due
to its single-operation nature.

c) Examine the below code to find its output. [An]

Java

int a = 1;

int b = 2;

int c = 3;

a += 5; // a = a + 5; a = 1 + 5 = 6

b *= 4; // b = b * 4; b = 2 * 4 = 8
c += a * b; // c = c + (a * b); Precedence: * before +=. c = 3 + (6 * 8) = 3 + 48 = 51

c %= 6; // c = c % 6; c = 51 % 6 = 3

// 51 / 6 = 8 with a remainder of 3

[Link]("a=" + a);

[Link]("b=" + b);

[Link]("c=" + c);

a=6

b=8

c=3

3. Increment and Decrement Operators

a) What are increment and decrement operators? [R]

 The increment operator (++) increases the value of its operand by $\mathbf{1}$.

 The decrement operator (--) decreases the value of its operand by $\mathbf{1}$.

Both operators can be used in two forms: prefix (e.g., ++a) or postfix (e.g., a++).

b) Compare pre and post increment operators. [U]

Feature Pre-Increment (++a) Post-Increment (a++)

The operand's value is incremented The operand's original value is used


Timing
first. in the expression first.

The operator evaluates to the value The operator evaluates to the value
Result
after the increment. before the increment.

++a is equivalent to a = a + 1; followed a++ is equivalent to use a; followed by


E ect
by use a;. a = a + 1;.

c) Examine the below code snippet to find its output. [An]

Java

int a = 1;

int b = 2;
int c, d;

// c = ++b; (Pre-increment)

// 1. b is incremented: b becomes 3

// 2. c is assigned the new value of b: c becomes 3

c = ++b;

// d = a++; (Post-increment)

// 1. d is assigned the current value of a: d becomes 1

// 2. a is incremented: a becomes 2

d = a++;

// c++; (Post-increment)

// 1. c is used (but not in an expression for assignment)

// 2. c is incremented: c becomes 4

c++;

[Link]("a=" + a); // a = 2

[Link]("b=" + b); // b = 3

[Link]("c=" + c); // c = 4

[Link]("d=" + d); // d = 1

a=2

b=3

c=4

d=1

4. Bitwise Operators

a) What are bitwise operators in Java? [R]


Bitwise operators are operators in Java that work directly on the individual bits (binary
digits) of integer data types (like long, int, short, char, and byte). They perform low-level
operations like bitwise AND, OR, XOR, NOT, and bit shifts.

The Bitwise Operators in Java are:

 Bitwise AND (&)

 Bitwise OR (|)

 Bitwise XOR (^)

 Bitwise Complement (~)

 Signed Left Shift (<<)

 Signed Right Shift (>>)

 Unsigned Right Shift (>>>)

b) Interpret how decimal is converted to a binary value while performing the bitwise
operations. [U]

Before a bitwise operation is performed on integer operands, the following happens:

1. The decimal numbers are converted into their binary representation (a


sequence of 0s and 1s).

2. Java promotes smaller integer types (byte, short, char) to int before performing
the bitwise operation. The standard int type is represented using 32 bits.

3. For positive numbers, the binary equivalent is straightforward (e.g., $2_{10} =


\dots0010_2$, $6_{10} = \dots0110_2$).

4. For negative numbers, the number is typically represented in Two's


Complement form (see 5.b).

5. The bitwise operation (like $\&$, $|$, or $\wedge$) is then applied bit-by-bit to
the binary representations of the operands.

6. The resulting binary number is converted back to its decimal equivalent to


produce the final output.

c) Examine the below code snippet to find its output. [An]

Let $a=2$ and $b=6$. In 32-bit binary:

$a = 2 \implies 0\dots0010$

$b = 6 \implies 0\dots0110$
Operation Binary Calculation Result Binary Result Decimal

$a \| $ (OR) $0\dots0010 \| 0\dots011$ $0\dots0110$ $6$

$a \& b$ (AND) $0\dots0010 \& 0\dots0110$ $0\dots0010$ $2$

$a \wedge b$ $0\dots0010 \wedge


$0\dots0100$ $4$
(XOR) 0\dots0110$

$-3$ (Two's
$\sim a$ (NOT) $\sim 0\dots0010$ $1\dots1101$
Comp)

$-7$ (Two's
$\sim b$ (NOT) $\sim 0\dots0110$ $1\dots1001$
Comp)

$res1 = \sim a \&


$1\dots1101 \& 0\dots0110$ $0\dots0100$ $4$
b$

$res2 = a \& \sim


$0\dots0010 \& 1\dots1001$ $0\dots0000$ $0$
b$

$res1 \| res$ $0\dots0100 \| 0\dots000$ $0\dots0100$ $4$

$res1 \& res2$ $0\dots0100 \& 0\dots0000$ $0\dots0000$ $0$

Java

[Link](a + "|" + b + ":" + (a | b));

[Link](a + "&" + b + ":" + (a & b));

[Link](a + "^" + b + ":" + (a ^ b));

int res1 = ~a & b;

int res2 = a & ~b;

[Link]("res1=" + res1);

[Link]("res2=" + res2);

[Link](res1 + "|" + res2 + ":" + (res1 | res2));

[Link](res1 + "&" + res2 + ":" + (res1 & res2));

2|6:6

2&6:2

2^6:4
res1=4

res2=0

4|0:4

4&0:0

5. Ones and Twos Complement

a) What is ones complement. [R]

One's Complement is a way of representing negative binary numbers. The one's


complement of a binary number is obtained by inverting every bit (or performing the
bitwise NOT operation). This means every $\mathbf{0}$ becomes a $\mathbf{1}$, and
every $\mathbf{1}$ becomes a $\mathbf{0}$.

b) Explain how negative numbers are represented using twos complement. [U]

Two's Complement is the standard way negative numbers are represented in Java (and
most modern computer systems) for signed integer types. It makes arithmetic
operations (addition and subtraction) easier and more e icient for the CPU.

To find the Two's Complement representation of a negative decimal number, say $-N$,
you follow these steps:

1. Find the binary representation of the positive number $\mathbf{N}$ (e.g., for $-
6$, find binary of $6$).

2. Find the One's Complement of that binary number (invert all the bits).

3. Add $\mathbf{1}$ to the One's Complement result.

The leftmost bit (Most Significant Bit or MSB) serves as the sign bit: $\mathbf{0}$ for
positive and $\mathbf{1}$ for negative.

Example: Representing $-6$ in an 8-bit system:

1. Binary of $6$: $0000\ 0110$

2. One's Complement: $1111\ 1001$

3. Add 1: $1111\ 1001 + 1 = 1111\ 1010$

So, $1111\ 1010$ is the Two's Complement representation of $-6$.

c) Examine the below code snippet to find its output. [An]

Let $a=-2$ and $b=6$. In 32-bit binary:


$a = -2 \implies 1\dots1110$ (Two's Complement)

$b = 6 \implies 0\dots0110$

Result
Operation Binary Calculation Result Binary
Decimal

$a \| $ (OR) $1\dots1110 \| 0\dots011$ $1\dots1110$ $-2$

$a \& b$ (AND) $1\dots1110 \& 0\dots0110$ $0\dots0110$ $6$

$1\dots1110 \wedge
$a \wedge b$ (XOR) $1\dots1000$ $-8$
0\dots0110$

$\sim a$ (NOT) $\sim 1\dots1110$ $0\dots0001$ $1$

$\sim b$ (NOT) $\sim 0\dots0110$ $1\dots1001$ $-7$

$res1 = \sim a \&


$0\dots0001 \& 0\dots0110$ $0\dots0000$ $0$
b$

$res2 = a \& \sim


$1\dots1110 \& 1\dots1001$ $1\dots1000$ $-8$
b$

$res1 \| res$ $0\dots0000 \| 1\dots100$ $1\dots1000$ $-8$

$res1 \& res2$ $0\dots0000 \& 1\dots1000$ $0\dots0000$ $0$

Java

[Link](a + "|" + b + ":" + (a | b));

[Link](a + "&" + b + ":" + (a & b));

[Link](a + "^" + b + ":" + (a ^ b));

int res1 = ~a & b;

int res2 = a & ~b;

[Link]("res1=" + res1);

[Link]("res2=" + res2);

[Link](res1 + "|" + res2 + ":" + (res1 | res2));

[Link](res1 + "&" + res2 + ":" + (res1 & res2));

-2|6:-2
-2&6:6

-2^6:-8

res1=0

res2=-8

0|-8:-8

0&-8:0

6. Relational Operators

a) List the relational operators available in Java. [R]

Relational operators are used to determine the relationship (like equality or magnitude)
between two operands.

The relational operators in Java are:

 Greater than (>)

 Less than (<)

 Greater than or equal to (>=)

 Less than or equal to (<=)

 Equal to (==)

 Not equal to (!=)

b) Interpret the return type of relational operators. [U]

The return type of all relational operators is a boolean value. They evaluate the
comparison between the two operands and return either:

 true (if the relationship holds)

 false (if the relationship does not hold)

c) Examine the following code snippet to find its output. [An]

Java

int a = 4, b = 5;

// [Link](a < b);

// 4 < 5 is true.
[Link](a < b);

// [Link](a > b);

// 4 > 5 is false.

[Link](a > b);

// [Link]((a++ < b) || (a++ == b));

// 1. (a++ < b): 4 < 5 is TRUE. a is then incremented to 5.

// 2. Short-circuit OR (||): Since the left operand is TRUE, the right operand (a++ == b) is
NOT evaluated.

// 3. The overall expression result is TRUE.

[Link]((a++ < b) || (a++ == b));

// [Link]("a=" + a);

// The last operation that changed 'a' was 'a++' in the short-circuited OR expression,
changing it from 4 to 5.

[Link]("a=" + a);

true

false

true

a=5

You might also like