Monday, October 1, 2018

70+ Java Coding Interview Questions

Java Coding Interview Questions

Java Coding Example
Java Coding Example









Page No:-8

306.What is the output of this program?
 class Java 
 {
  public static void main(String args[]) 
{
      int  i = 280;
      double j = 285.25;
      byte k = (byte) i;
      byte l = (byte) j;
      System.out.println(k + " "  + l);
  } 
 }
a. 285.25 280  
b. 24 29   
c. 295 280
d. None of the above


307. What will be the output of the following program?

class Avi
{
public static void main(String args[]) 
{
    char a = 'X';
    char b = 65;
    a++;
    b++;
    System.out.println(a + " "  + b);

}
a. Y B    
b. A y
c. X A 
d. None of the above

308. Which answer is correct of the following code?

public static void main(String args[])
{
int a=6,d=3,b=4,e=1;
System.out.println (--a * ( b -d ) +e);
}
a. 4
b. 5
c. 6    
d. 7

309. What is the output of the following java code?

class Java
 {
  public static void main (string args[ ]) {
  int y=4;
  System.out.println("y>>>1=" +(y>>>1));
  }
  }
a. x>>>1=2   
b. x>>>1=3
c. x>>>1=1
d. None of Above

310. What will be the output of this program?

public static void main(String args[])
{
String str1 = "JAVA";
String str2 = "JAVA";
System.out.println(str1.equals(str2));
}
Output will be?
a. Hello equals Hello -> 0
b. Hello equals Hello -> 1
c. true    
e. Hello equals Hello -> hello.


311. How to print "Hello World" in java.
             
         public class Java
       {
       public static void main(String[] args) 
       {
       System.out.println("HELLO WORLD");
       }
       }
output:-     HELLO WORLD

312.What will be the output of this program?

 public class Java 
 {
 public static void main(String[] args) 
 {
int i = 2;
while (i < 6) {
if( i++ == 2 ) 
 System.out.print("Java");
 if( ++i == 4)
System.out.print("Programming");
else
System.out.print("Language");
}
 }
 }

a. JavaProgramminglanguage   
b. ProgramminglanguageJava
c. run-time error
d. compile-time error


313. What is the output of the following code?

class Java 
{
public static void main (String args[]) 
{
int x=8, y=4;
System.out.println("x|y=" +(x|y));
System.out.println("x&y=" +(x &y));
System.out.println("x^y=" +(x^y));
  }
  }

a. x&y=6
    x|y=7
    x^y=3

b. x&y=4
    x&y=0
    x^y=3

c.  x|y=12   
     x&y=0
     x^y=12

d. x&y=4
    x|y=8
    x^y=3


314. What is the output of the following code?

class JAVA {
public static void main (String args[]) 
{
 int x=21, y=25;
 System.out.println("x ="+ ++x);
System.out.println("y ="+ y++);
System.out.println("z ="+ (x+y));
}
}

a. x=22  y=25  z=48   
b. x=22  y=24  z=46
c. x=24  y=25  z=49
d. None Of Above.


315. What will be the output of the following program?

class Avi
{
public static void main(String[] args) 
{
    int i, j, l = 1;
        for (i = 1; i <= 4; i++) {
            for (j = 1; j < i + 1; j++) {
                System.out.print(l++ + " ");
    }
     System.out.println();
     }
    }
 }

the output will be:-
a. 1     
    2 3 
    4 5 6 
    7 8 9 10 

b. 1 2 3 4 5 6 7 8 9 10

c. The program will not be run.


316. Check the number is even or odd.

import java.util.Scanner;
class EvenOdd
{
  public static void main(String args[])
  {
    int number;
    System.out.println("Please Enter Integer number:");
    Scanner sc = new Scanner(System.in);
    number = sc.nextInt();//here number is storing. 

    /* we know that if number is divisible by 2 then it's an even number
     , else odd number*/
    if ( number % 2 == 0 )
        System.out.println(number+" is even number.");
     else
        System.out.println(number+" is odd number.");
  }
}

Example:-
Please Enter an Integer number:
45
45 is an odd number


317. Check any number is PRIME or NOT.

import java.util.Scanner;
class CheckPrime
{
   public static void main(String args[])
   {
int temp;
boolean prime=true;
Scanner sc= new Scanner(System.in);
System.out.println("Please Enter any number:");
int number=sc.nextInt();
 for(int i=2;i<=number/2;i++)
 {
  temp=number%i;
 if(temp==0)
 {
  prime=false;
  break;
 }
 } 
if(prime)
   System.out.println(number + " is a Prime Number");
else
   System.out.println(number + " is not a Prime Number");
   }
   }

Example:-
Please Enter any number:
17
17 is a Prime Number


318. Check any year is Leap year or not.

import java.util.Scanner;
public class Avi 
    {
     public static void main(String[] args) 
    {
     int year;
     Scanner sc = new Scanner(System.in);
     System.out.println("Please Enter any Year");
     year = sc.nextInt();
     if(year % 4 == 0 || year % 100 == 0  || year % 400 == 0)
        {
         System.out.println(year + " is a Leap Year.");
        }
         else
        {
        System.out.println(year + " is not a Leap Year.");
        }
        }
        }
        
Example:-
      Please Enter any Year
      2006
      2006 is not a Leap Year

319. Check the Number is Armstrong or not by While Loop.

import java.util.Scanner;
public class Armstrong {
public static void main(String[] args)
       {
int sum=0,temp,num;
System.out.println("Please Enter any Number");
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
num=number;
while(num != 0)
{
temp=num%10;
sum= sum+temp*temp*temp;
num = num/10;     //you can write also "num /= 10;"
}
if(number==sum)
{
System.out.println(number+" is Armstrong number.");
}
else
{
System.out.println(number+" is not Armstrong number.");
}
 }
}


320. Example of method OVERLOADING.

class MethodOverLoading 
{
void display(int a,int b)
{
System.out.println("a="+a+" "+"b="+b);
}
void display(int a,int b,int c)
{
System.out.println("a="+a+" "+"b="+b+" "+"c="+c);
}
 }
public class Avi
{
public static void main(String[] args) 
{
MethodOverLoading mol = new MethodOverLoading();
mol.display(5, 10);
mol.display(15, 20, 25);
}
}

output:-  a=5 b=10
              a=15 b=20 c=25

[  Ans:-  306.b   307.a   308.c   309.a   310.c    311.a   312.b   313.c   314.a   315.a  ]
PRENEXTHOME
page 1
page 2

page 3
page 4

page 5
page 6
                         
page 7


Disqus Comments