Java Programming Questions and Answers for Written Test: Most important Java Programming Questions and Answers frequently asked in Java Coding Test for Interview.
Java Programming Questions and Answers for Written Test
Q:- Write a program to print "Hello World" in Java
public class PrintMessage {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
//Output: Hello World
Q:- Write a program to print "Hello World" without using main method in Java
public class PrintMessage {
static {
System.out.println("Hello World");
System.exit(0);
}
}
//Note -
//1. The System.exit(0) exits the program before the JVM starts to look for main()
//2. This will not work with JDK7 - Because, In JDK7 the code would not execute as it looks for the main method before any other thing.
//Output: Hello World
You may also like Core Java Interview Questions
Q:- Write a factorial program using recursion in Java
public class Factorial {
static int calFactorial(int n){
if (n == 0){
return 1;
}else {
return(n * calFactorial(n-1));
}
}
public static void main(String args[]){
int number = 5;
int factOutput = 1;
factOutput = calFactorial(number);
System.out.println("Factorial of the number "+number+" is: "+factOutput);
}
}
//Output: Factorial of the number 5 is: 120
Q:- Write a program to sum of two numbers in Java
public class SumClass {
public static void main(String args[]) {
int x=10;
int y=20;
int z=x+y;
System.out.println("Sum of x+y = " + z);
}
}
//Output: Sum of x+y = 30
Q:- Write a program to check whether the given number is even or odd in Java
import java.util.Scanner;
public class CheckNumberEvenOdd {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter your number: ");
int num = reader.nextInt();
if(num % 2 == 0)
System.out.println(num + " is an even number.");
else
System.out.println(num + " is an odd number.");
}
}
//Note - Scanner object, reader is created to read a number from the user's keyboard.
You may also like Oracle DBA Interview Questions
Q:- Write a program to print fibonacci series based on the user input in Java
import java.util.Scanner;
public class FibonacciSeries {
public static void main(String[] args) {
int count, num1 = 0, num2 = 1;
System.out.println("How many numbers you want in the series:");
Scanner scanner = new Scanner(System.in);
count = scanner.nextInt();
scanner.close();
System.out.print("Fibonacci Series of "+count+" numbers:");
int i=1;
while(i<=count){
System.out.print(num1+" ");
int sumOfPrevTwo = num1 + num2;
num1 = num2;
num2 = sumOfPrevTwo;
i++;
}
}
}
//Output - Fibonacci Series of 10 numbers:0 1 1 2 3 5 8 13 21 34
OR
public class FibonacciSeries {
public static void main(String args[]) {
int n1 = 0, n2 = 1, no, i, count = 10;
System.out.print("Fibonacci Series of " + count + " numbers:" + n1 + " " + n2);
for (i = 2; i <
count; ++i) {
no = n1 + n2;
System.out.print(" " + no);
n1 = n2;
n2 = no;
}
}
}
//Output - Fibonacci Series of 10 numbers:0 1 1 2 3 5 8 13 21 34
Java Programming Questions and Answers for Written Test:
java coding test for interview: Following are some java questions for your practice.
Q:- Write a program to swap two numbers without using 3rd variable in Java.
Q:- Write a program to get the prime numbers between a given range in Java.
Q:- Write a program to check whether a number is prime or not in Java.
Q:- Write a program to check whether a number is armstrong or not in Java.
Q:- Write a program to check whether a string is palindrome or not in Java.