- In most of the interviews questions on increment and decrement operators will come in face to face or in written test.
- Every Java Interview written test will have compulsory one question on increment and decrements operators.
- Lets see some of the frequently asking java interview programming questions on increment and decrement operators.
- I this pre increment and post increment , pre decrement and post decrement topics will cover in below programs.
- Normal programs: use of increment and decrement in normal scenario is same.
- They differ in while we use them in expressions.
- To avoid confusions for basic java learners we provided individual programs.
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);
- }
- }
- 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);
- }
- }
- 11
3.Java Interview program on pre increment operator in expression.
- package com.instanceofjava;
- class IncrementDemo{
- public static void main(String [] args){
- int a=20;
- a= ++a + 1;
- System.out.println(a);
- }
- }
- 22
4.Java Interview program on post increment operator in expression.
- package com.instanceofjava;
- class IncrementDemo{
- public static void main(String [] args){
- int a=20;
- a= a++ + 1;
- System.out.println(a);
- }
- }
- 21
5.Java Interview program on pre increment operator in expression.
- package com.instanceofjava;
- class IncrementDemo{
- public static void main(String [] args){
- int a=20;
- a= ++a + ++a;
- System.out.println(a);
- }
- }
- 43
6.Java Interview program on post increment operator in expression.
- package com.instanceofjava;
- class IncrementDemo{
- public static void main(String [] args){
- int a=20;
- a= a++ + a++;
- System.out.println(a);
- }
- }
- 41
7.Java Interview program on pre and post increment operator in expression.
- package com.instanceofjava;
- class IncrementDemo{
- public static void main(String [] args){
- int a=20;
- a= a++ + ++a;
- System.out.println(a);
- }
- }
8.Java Interview program on pre decrement
- package com.instanceofjava;
- class IncrementDemo{
- public static void main(String [] args){
- int a, b;
- a=10;
- b=--a;
- System.out.println(b);
- }
- }
- 9
9.Java Interview program on post decrement .
- package com.instanceofjava;
- class IncrementDemo{
- public static void main(String [] args){
- int a, b;
- a=30;
- b=a--;
- System.out.println(a);
- }
- }
- 29
10.Java Interview program on pre decrement in expression.
- package com.instanceofjava;
- class IncrementDemo{
- public static void main(String [] args){
- int a=20;
- a= --a - 1;
- System.out.println(a);
- }
- }
- 18
11.Java Interview program on post decrement in expression.
- package com.instanceofjava;
- class IncrementDemo{
- public static void main(String [] args){
- int a=20;
- a= a-- - 1;
- System.out.println(a);
- }
- }
- 19
12.Java Interview program on pre decrement in expression.
- package com.instanceofjava;
- class IncrementDemo{
- public static void main(String [] args){
- int a=20;
- a= --a - --a;
- System.out.println(a);
- }
- }
- 1
13.Java Interview program on post decrement operator in expression.
- package com.instanceofjava;
- class IncrementDemo{
- public static void main(String [] args){
- int a=20;
- a= a-- - a--;
- System.out.println(a);
- }
- }
- 1
14.Java Interview program on pre and post decrement operator in expression.
- package com.instanceofjava;
- class IncrementDemo{
- public static void main(String [] args){
- int a=20;
- a= a-- - --a;
- System.out.println(a);
- }
- }
- 2
15.Java Interview program on increment and decrement operator in expression.
- package com.instanceofjava;
- class IncrementDemo{
- public static void main(String [] args){
- int a=20;
- a= a-- + ++a;
- System.out.println(a);
- }
- }
- 40
16.increment and decrement operators Inside println() method:
- package com.instanceofjava;
- class IncrementDemo{
- public static void main(String [] args){
- int a=1;
- System.out.println(a++);
- System.out.println(a++);
- System.out.println(++a);
- System.out.println(a++);
- System.out.println(a++);
- System.out.println(a--);
- System.out.println(a--);
- System.out.println(--a);
- System.out.println(--a);
- System.out.println(a--);
- }
- }
17.Java Interview program on increment and decrement operator in println
- package com.instanceofjava;
- class IncrementDemo{
- public static void main(String [] args){
- int i=15;
- System.out.println(i++);
- System.out.println(i--);
- }
- }
- 15
- 16
18.Java Interview program on increment and decrement operator in println
- package com.instanceofjava;
- class IncrementDemo{
- public static void main(String [] args){
- int a=10,b=10;
- for(int i=0;i<5;i++){
- if(++a>2||++b>2){
- a++;
- }
- }
- System.out.println("a= "+a+" b="+b);
- }
- }
- 20
- 10
19.Java Interview program on increment and decrement operator in loops
- package com.instanceofjava;
- class IncrementDemo{
- public static void main(String [] args){
- int i;
- for( i=1;i<4;i++){
- System.out.println(i);
- }
- System.out.println("value of i after completion of loop: "+i);
- }
- }
- 1
- 2
- 3
- value of i after completion of loop: 4
20.Java Interview program on increment and decrement
- package com.instanceofjava;
- public class A{
- static int a = 1111;
- static
- {
- a = a-- - --a;
- }
- {
- a = a++ + ++a;
- }
- public static void main(String[] args) {
- System.out.println(a);
- }
- }
Output:
- 2
No comments:
Post a Comment