Top 50 Coding Questions Asked in Placements
Top 50 Coding Questions Asked in Placements
int reverse = 0;
String s = "";
while (number != 0)
{
int pick_last = number % 10;
sc.close();
}
2. Write the code to find the Fibonacci series upto the nth term.
public class Main {
public static void main(String[] args) {
int num = 15;
int a = 0, b = 1;
int nextTerm;
}
}
import java.util.Scanner;
public class gcd_or_hcf {
public static void main(String[] args) {
//scanner class declaration
Scanner sc = new Scanner(System.in);
//input from the user
System.out.print("Enter the first number : ");
int num1 = sc.nextInt();
//input from the user
System.out.print("Enter the second number : ");
int num2 = sc.nextInt();
int n = 1;
System.out.print("HCF of " + num1 + " and " + num2 + " is ");
if (num1 != num2) {
while (n != 0) {
//storing remainder
n = num1 % num2;
if (n != 0) {
num1 = num2;
num2 = n;
}
}
//result
System.out.println(num2);
} else
System.out.println("Wrong Input");
//closing scanner class(not compulsory, but good practice)
sc.close();
}
}
4. Write code of Perfect number
import java.util.Scanner;
public class perfect_number_or_not
{
public static void main(String[] args)
{
//scanner class declaration
Scanner sc = new Scanner(System.in);
//input from user
System.out.print("Enter a number : ");
int number = sc.nextInt();
//declare a variable to store sum of factors
int sum = 0;
for(int i = 1 ; i < number ; i++)
{
if(number % i == 0)
sum = sum + i;
}
//comparing whether the sum is equal to the given number or not
if(sum == number)
System.out.println("Perfect Number");
else
System.out.println("Not an Perfect Number");
//closing scanner class(not compulsory, but good practice)
sc.close();
}
}
import java.util.Arrays;
import java.util.Scanner;
public class CheckIfTwoStringsAreAnagramAreNot {
static boolean isAnagram(String str1, String str2) {
String s1 = str1.replaceAll("[\\s]", "");
String s2 = str2.replaceAll("[\\s]", "");
boolean status = true;
if (s1.length() != s2.length())
status = false;
else {
char[] a1 = s1.toLowerCase().toCharArray();
char[] a2 = s2.toLowerCase().toCharArray();
Arrays.sort(a1);
Arrays.sort(a2);
status = Arrays.equals(a1, a2);
}
return status;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter two String :");
String s1 = sc.next();
String s2 = sc.next();
boolean status = isAnagram(s1, s2);
if (status)
System.out.println(s1 + " and " + s2 + " are Anagram");
else
System.out.println(s1 + " and " + s2 + " are not Anagram");
}
}
import java.util.Scanner;
import java.util.Scanner;
8. Write code to check if two strings match where one string contains wildcard
characters
// If the first character is '?' or the first characters of both strings match
if ((n > 1 && a.charAt(0) == '?') || (n != 0 && m != 0 && a.charAt(0) == b.charAt(0))) {
return solve(a.substring(1), b.substring(1));
}
// If none of the above conditions are met, the strings do not match
return false;
}
int temp;
for (int i = 0; i < size - 1; i++) {
// Since, after each iteration rightmost i elements are sorted
for (int j = 0; j < size - i - 1; j++) {
if (array[j] > array[j + 1]) {
// Swap the elements
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
System.out.println("After bubble sort: ");
display(array);
}
}
11. Write to code to check whether a given year is leap year or not.
import java.util.Scanner;
System.out.println("Enter Year:");
int year = scanner.nextInt();
if (year % 400 == 0) {
System.out.println(year + " is a Leap Year");
} else if (year % 4 == 0 && year % 100 != 0) {
System.out.println(year + " is a Leap Year");
} else {
System.out.println(year + " is not a Leap Year");
}
scanner.close();
}
}
import java.util.*;
class Solution
{
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
System.out.println ("Enter the string");
String str = sc.next (); //Taking input as a string from the user
int freq[] = new int[256];
15. Write a code to replace each element in an array by its rank in the array
import java.util.*;
class Main {
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
int[] arr = { 100, 2, 70, 12 , 90};
// Function Call
changeArr(arr);
class Main {
/*Function to left rotate arr[] of size n by d*/
static void leftRotate(int arr[], int d, int n) {
for (int i = 0; i < d; i++) leftRotatebyOne(arr, n);
}
static void leftRotatebyOne(int arr[], int n) {
int i, temp;
temp = arr[0];
for (i = 0; i < n - 1; i++) arr[i] = arr[i + 1];
arr[n - 1] = temp;
}
/* utility function to print an array */
static void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) System.out.print(arr[i] + " ");
}
// Driver program to test above functions
public
static void main(String[] args) {
// RotateArray rotate = new RotateArray();
int arr[] = {1, 2, 3, 4, 5};
leftRotate(arr, 2, 5);
printArray(arr, 5);
}
}
import java.util.Arrays;
class Main
{
public static void countFreq(int arr[], int n)
{
boolean visited[] = new boolean[n];
Arrays.fill(visited, false);
// Count frequency
int count = 1;
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
visited[j] = true;
count++;
}
}
if(count==1)
System.out.println(arr[i]);
}
// Driver code
public static void main(String []args)
{
int arr[] = new int[]{10, 30, 40, 20, 10, 20, 50, 10};
int n = arr.length;
countFreq(arr, n);
}
}
import java.util.*;
class Main
{
// Function to check if n is palindrome
static boolean isPalindrome(int n)
{
// Find the appropriate divisor
// to extract the leading digit
int divisor = 1;
while (n / divisor >= 10)
divisor *= 10;
while (n != 0) {
int x = n / divisor;
int y = n % 10;
// Driver program
public static void main(String []args)
{
int []A = { 121, 2322, 54545, 999990 };
int n = A.length;
if(num >= 0)
{
System.out.println(num + " Factorial: " + getFact(num));
}
else
System.out.println("Negative Number: No Factorial");
}
if(num == 1 || num == 0)
return 1;
// check if Armstrong
if (armstrong (num, len))
System.out.println(num + " is armstrong");
else
System.out.println(num + " is armstrong");
21. Write a program to find the sum of Natural Numbers using Recursion.
public class Main
{
public static void main (String[]args)
{
int n = 10;
int sum = getSum (n);
System.out.println (sum);
}
scanner.close();
}
}
23. Write a Program to Find the Sum of Natural Numbers using Recursion.
int n = 10;
int sum = getSum (n);
System.out.println (sum);
}
import java.util.Scanner;
public class Palindrome{
reader.close();
return input.equals(reverseOfString);
}
//JAVA Program to check whether the character entered by user is Vowel or Consonant.
import java.util.Scanner;
public class vowelorconsonant
{
//class declaration
public static void main(String[] args)
{
//main method declaration
Scanner sc=new Scanner(System.in); //scanner class object creation
System.out.println(" Vowel");
else if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) //condition for the
consonants
System.out.println(" Consonant");
else
System.out.println(" Not an Alphabet");
}
}
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
//scanner class object creation
Scanner sc=new Scanner(System.in);
29. Write a code to Remove all characters from string except alphabets
import java.util.Scanner;
class RemoveCharactersInAtringExceptAlphabets {
import java.util.Scanner;
System.out.print(min);
}
}
31. Write a code to Reverse the element of the array
import java.util.Scanner;
int n=arr.length;
for(int i=n-1; i>=0; i--)
System.out.print(arr[i]+" ");
}
}
//Initialize array
int [] arr = new int [] {10, 40, 30, 20};
int temp = 0;
//Initialize array
int temp = 0;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
import java.util.Scanner;
public
class Main {
static int arr[] = new int[]{1, 6, 4, 5};
static int getInvCount(int n) {
int inv_count = 0;
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++) if (arr[i] > arr[j]) inv_count++;
return inv_count;
}
// Driver method to test the above function
public
static void main(String[] args) {
System.out.println("Number of inversions are " + getInvCount(arr.length));
}
}
import java.io.*;
import java.util.*;
public
class Main {
static int findLongestConseqSubseq(int arr[], int n)
{
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 9, 3, 10, 4, 20, 2 };
int n = arr.length;
System.out.println(
"Length of the Longest "
+ "contiguous subsequence is "
+ findLongestConseqSubseq(arr, n));
}
}
//output
System.out.println ("Sum of digits : " + sum);
}
{
public static void main(String[] args) {
//output
System.out.println ("Sum of digits : " + sum);
}
System.out.print(max);
}
}
int d = b * b - 4 * a * c;
double sqrt_val = sqrt(abs(d));
if (d > 0) {
System.out.println("Roots are real and different");
System.out.println((double)(-b + sqrt_val) / (2 * a) + "\n"+ (double)(-b -
sqrt_val) / (2 * a));
}
else if (d == 0) {
System.out.println("Roots are real and same ");
System.out.println(-(double)b / (2 * a) + "\n" + -(double)b / (2 * a));
}
else // d < 0
{
System.out.println("Roots are complex");
// Driver code
public static void main(String args[])
{
int a = 1, b = 4, c = 4;
// Function call
findRoots(a, b, c);
}
}
class Main {
// Base cases
if (len == 0) {
System.out.println("empty string");
return;
}
if (len > 4) {
System.out.println(
"Length more than 4 is not supported");
return;
}
if (len == 1) {
System.out.println(single_digits[num[0] - '0']);
return;
}
int x = 0;
while (x < num.length) {
if (len >= 3) {
if (num[x] - '0' != 0) {
System.out.print(single_digits[num[x] - '0'] + " ");
System.out.print(tens_power[len - 3] + " ");
}
--len;
}
else {
if (num[x] - '0' == 1) {
int sum
= num[x] - '0' + num[x + 1] - '0';
System.out.println(two_digits[sum]);
return;
}
else {
int i = (num[x] - '0');
if (i > 0)
System.out.print(tens_multiple[i] + " ");
else
System.out.print("");
++x;
if (num[x] - '0' != 0)
System.out.println(single_digits[num[x] - '0']);
}
}
++x;
}
}
// Driver Code
public static void main(String[] args)
{
convert_to_words("1121".toCharArray());
}
}
46: Write a Program to Find the Factorial of a Number using Recursion.
class Main {
// method to find factorial of given number
static int factorial(int n)
{
if (n == 0)
return 1;
// Driver method
public static void main(String[] args)
{
int num = 5;
System.out.println("Factorial of " + num + " is " + factorial(5));
}
}
int n=arr.length;
for(int i=n-1; i>=0; i--)
System.out.print(arr[i]+" ");
}
}
48. Write code to check if two strings match where one string contains
wildcard characters
public class WildcardMatching {
if (n == 0 && m == 0) {
return true;
}
if (n > 1 && a.charAt(0) == '*' && m == 0) {
return false;
}
if ((n > 1 && a.charAt(0) == '?') || (n != 0 && m != 0 && a.charAt(0) ==
b.charAt(0))) {
return solve(a.substring(1), b.substring(1));
}
if (n != 0 && a.charAt(0) == '*') {
return solve(a.substring(1), b) || solve(a, b.substring(1));
}
return false;
}
class Main{
static int R = 4;
static int C = 4;
if (i >= m || j >= n) {
return;
}
if ((m - 1) != i) {
for (int p = n - 2; p >= j; p--) {
System.out.print(arr[m - 1][p] + " ");
}
}
if ((n - 1) != j) {
for (int p = m - 2; p > i; p--) {
System.out.print(arr[p][j] + " ");
}
}
print(arr, i + 1, j + 1, m - 1, n - 1);
}
print(a, 0, 0, R, C);
}
}