In this tutorial we will see about few simple interview programming questions.
1. Sort 3 numbers without IF condition or looping.
public class Sort3No {
public static void main(String[] args) {
int x = 9;
int y = 3;
int z = 7;
int first = Math.min(x, Math.min(y, z));
int third = Math.max(x, Math.max(y, z));
int second = x + y + z - first - third;
System.out.println("SORTED NO's : " + first + ", " + second + ", "+ third);
}
}
OUTPUT:
SORTED NO's : 3, 7, 9
2. Reverse the number without using String
public class ReverseNumber {
public static void main(String[] args) {
int number = 123456;
int revNo = 0;
while(number > 0){
revNo = (10 * revNo) + (number % 10);
number = number / 10;
}
System.out.println("REVERSED NO : "+revNo);
}
}
OUTPUT:
REVERSED NO : 654321
3. Print first N Fibonacci numbers.
public class Fibonacci {
public static void main(String[] args) {
int input = 10;
int fib = 0, val= 1;
System.out.print("First "+input+" Fibonacci Numbers : ");
for (int i = 0; i < input; i++) {
fib = fib + val;
val = fib - val;
System.out.print(fib+", ");
}
}
}
OUTPUT:
First 10 Fibonacci Numbers : 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,
4. Convert String to Date Object
public class StringToDate {
public static void main(String args[]) {
try{
String strDate = "23-5-2013";
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
Date date = format.parse(strDate);
System.out.println("DATE : "+date);
}catch (ParseException e) {
e.printStackTrace();
}
}
}
OUTPUT:
DATE : Thu May 23 00:00:00 IST 2013
5. Find entered date is valid or not
public class ValidDate {
public static void main(String[] args) {
int day = 29;
int month = 2;
int year = 1984;
int[] leapYearDays = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int[] nonLeapYearDays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month >= 1 && month <= 12) {
if (year % 4 == 0) {
if (day >= 1 && day <= leapYearDays[month - 1]) {
System.out.println("Valid Date...");
} else {
System.out.println("Invalid Date");
}
} else {
if (day >= 1 && day <= nonLeapYearDays[month - 1]) {
System.out.println("Valid Date...");
} else {
System.out.println("Invalid Date");
}
}
} else {
System.out.println("Invalid Date");
}
}
}
OUTPUT:
Valid Date...
6. Find the day of week by given date.
public class DayOfWeek {
public static void main(String[] args) {
int day = 23;
int month = 5;
int year = 2013;
String days[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
int a = year - (14 - month) / 12;
int x = a + a / 4 - a / 100 + a / 400;
int b = month + 12 * ((14 - month) / 12) - 2;
int c = (day + x + (31 * b) / 12) % 7;
System.out.println(days[c]);
}
}
OUTPUT:
Thursday
7. Print 5 random numbers between 2 numbers
public class PrintRandom {
public static void main(String[] args) {
int min = 100;
int max = 200;
for(int i=0;i<5;i++){
int rand = new Random().nextInt(max);
if(rand > min){
System.out.println(rand);
}else{
i--;
}
}
}
}
OUTPUT:
122
180
193
114
187
8. Replace character in a string without using Java API functions
public class ReplaceChar {
public static void main(String[] args) {
String str = "This document is the API specification for version 6 of the Java";
System.out.println("INPUT : "+str);
str = replaceChar(str, 'Q', 'i'); // Change all 'i' to 'Q'
System.out.println("AFTER CHAR REPLACE : "+str);
}
public static String replaceChar(String str, char newChar, char oldChar){
try{
char[] charAry = str.toCharArray();
for(int i=0;i<str.length();i++){
if(charAry[i] == oldChar){
charAry[i] = newChar;
}
}
str = new String(charAry);
}catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
OUTPUT:
INPUT : This document is the API specification for version 6 of the Java
AFTER CHAR REP: ThQs document Qs the API specQfQcatQon for versQon 6 of the Java
9. Write a Java program to find given number is odd or even without / or % operators.
public class OddOrEven {
public static void main(String[] args) {
int no = 25;
while(no>2){
no -= 2;
}
if(no == 1){
System.out.println("Given no. is Odd Number...");
}else{
System.out.println("Given no. is Even Number...");
}
}
}
OUTPUT:
Given no. is Odd Number...
10. This is a simple mathematical calculation program.
Example: Raj purchased the bag for Rs.2750 which includes 10% tax of bag price. So what will be the price of bag and tax amount?. Program should work for whatever be the amount as input.
public class CalculatePrice {
public static void main(String[] args) {
int price = 2750;
float bagPrice = ((price/11f)*10f);
float taxAmt = (price/11f);
System.out.println("BAG PRICE : "+bagPrice);
System.out.println("TAX AMOUNT : "+taxAmt);
System.out.println("TOTAL PRICE : "+(bagPrice+taxAmt));
}
}
OUTPUT:
BAG PRICE : 2500.0
TAX AMOUNT : 250.0
TOTAL PRICE : 2750.0
2 comments:
Write comments