Set of questions of increment and decrement operators in java ( write ans in bracket ) ( atleast 50 questions )
Answers
Answered by
1
hi mate,
Answer :
Increment and Decrement Operators in Java
- Increment and decrement operators in Java are also known as unary operators because they operate on a single operand. The increment operator (++) adds 1 to its operand and decrement operator (–) subtracts one.
- Operator Meaning
- ++ Increment Operator
- — Decrement Operator
- Syntax:-
- ++variable; // pre increment operator
- variable++; // post increment operator
- --variable; // pre decrement operator
- variable--; // post decrement operator
- Expression The initial
- The final
- value of a Value x
- x = ++a 4 5 5
- x = a++ 4 5 4
- x = –a 4 3 3
- x = a– 4 3 4
- Increment operators (++)
- First, the value of the variable a incremented by 1 and store in the memory location of variable a.
- Second, the value of variable a assign to the variable x.
- class PreIncrementOperator{
public static void main
(String[] args) {
int a, x;
a = 10;
x= ++a;
System.out.println("a: "+a);
System.out.println("x: "+x);
}
}
- Output:-
- a: 11
- x: 11
- Decrement Operators ( -- )
- Pre-decrement operator in Java ( --a )
- First, the value of a decremented by 1 and store in the memory location of variable a.
- Second, the value of the variable a assigned to the variable x.
- class PreDecrementOperator
{
public static void main(String[] args) {
int a, x;
a = 10;
x = --a;
System.out.println("a: "+a);
System.out.println("x: "+x);
}
}
- Output:-
- x: 9
- y: 9
i hope it helps you.
Answered by
13
Answer:
1.Java Interview program on pre increment operator.
- package com.instanceofjava;
- class IncrementDemo{
- public static void main(String [] args){
- int a, b;
- a=10;
- b=++a;
- System.out.println(b);
- }
- }
Output:
- 11
2.Java Interview program on post increment operator.
- package com.instanceofjava;
- class IncrementDemo{
- public static void main(String [] args){
- int a, b;
- a=10;
- b=a++;
- System.out.println(a);
- }
- }
Output:
- 11
❤️
Similar questions