You are on page 1of 15

-1-

Q.1) If else Ladder

public class Test{


   public static void main (String args[ ]) {
      int x = 30;

      if( x == 10 ) {
         System.out.print("Value of X is 10");
      }else if( x == 20 ) {
         System.out.print("Value of X is 20");
      }else if( x == 30 ) {
         System.out.print("Value of X is 30");
      }else {
         System.out.print("This is else statement");
   }
   }
}

Output window:

Value of X is 30

-2-
Q.2)Switch

(I)General

class Main {
public static void main(String[] args) {

int week = 4;
String day;

// switch statement to check day


switch (week) {
case 1:
day = "Sunday";
break;
case 2:
day = "Monday";
break;
case 3:
day = "Tuesday";
break;

// match the value of week


case 4:
day = "Wednesday";
break;
case 5:
day = "Thursday";
break;
case 6:
day = "Friday";
break;
case 7:
day = "Saturday";
break;
default:
day = "Invalid day";
break;
}
System.out.println("The day is " + day);
}
}

Output Window:

The day is wednesday

-3-
(ii)Loop

class Main {
public static void main(String[] args) {

int sum = 0;
int n = 1000;

// for loop
for (int i = 1; i <= n; ++i) {
// body inside for loop
sum += i; // sum = sum + i
}

System.out.println("Sum = " + sum);


}
}

Output Window

Sum=500500

-4-
(iii) Patterns

import java.util.Scanner;

public class Test


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in); //Taking rows value from the user
System.out.println("Enter the number of rows: ");
int rows = sc.nextInt();
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(i+" ");
}

System.out.println();
}
sc.close();
}
}

Output Window:

-5-
Q3)Function overloading

class Demo{
void disp(int a, double b){
System.out.println("Method A");
}
void disp(int a, double b, double c){
System.out.println("Method B");
}
public static void main(String args[]){
Demo obj = new Demo();
/* I am passing float value as a second argument but
* it got promoted to the type double, because there
* wasn't any method having arg list as (int, float)
*/
obj.disp(100, 20.67f);
}
}

Output Window:

Method A

-6-
Q4)Constructor (Parameterized)

class Example{
Example(int i, int j){
System.out.print("parameterized constructor");
}
Example(int i, int j, int k){
System.out.print("parameterized constructor");
}
public static void main(String args[]){
Example obj = new Example();
}
}

Output Window:

The constructor Example() is undefined

-7-
Q5)String

i)

class Main {
public static void main(String[] args) {

// create strings
String first = "java programming";
String second = "java programming";
String third = "python programming";

// compare first and second strings


boolean result1 = first.equals(second);
System.out.println("Strings first and second are equal: " + result1);

//compare first and third strings


boolean result2 = first.equals(third);
System.out.println("Strings first and third are equal: " + result2);
}
}

Output Window:

Strings first and second are equal: true


Strings first and third are equal: false

-8-
ii)

class Main {
public static void main(String[] args) {

// create string using the string literal


String greet = "Hello! World";
System.out.println("The string is: " + greet);

// returns the character at 3


System.out.println("The character at 3: " + greet.charAt(3));

// returns the character at 7


System.out.println("The character at 7: " + greet.charAt(7));
}
}

Output window:

The string is: Hello! World


The character at 3: l
The character at 7: W

-9-
Q6) Array

i)

class Main {
public static void main(String[] args) {

// create an array
int[] age = {12, 4, 5, 2, 5};

// access each array elements


System.out.println("Accessing Elements of Array:");
System.out.println("First Element: " + age[0]);
System.out.println("Second Element: " + age[1]);
System.out.println("Third Element: " + age[2]);
System.out.println("Fourth Element: " + age[3]);
System.out.println("Fifth Element: " + age[4]);
}
}

Output Window:

Accessing Elements of Array:


First Element: 12
Second Element: 4
Third Element: 5
Fourth Element: 2
Fifth Element: 5

-10-
ii)

int binarySearch(int arr[], int l, int r, int x)


{
    while (l <= r) {
        int m = l + (r - l) / 2;
  
              if (arr[m] == x)
            return m;
  
                    l = m + 1;
                  else
            r = m - 1;
    }
  
           return -1;
}
  
int main(void)
{
    int arr[] = { 2, 3, 4, 10, 40 };
    int x = 10;
    int n = sizeof(arr) / sizeof(arr[0]);
    int result = binarySearch(arr, 0, n - 1, x);
    (result == -1) ? cout << "Element is not present in array"
                   : cout << "Element is present at index " << result;
    return 0;

Output Window:

Element is present at index 3

-11-
iii)

public class SelectionSort {


   public static void main(String args[]){
      int array[] = {10, 20, 25, 63, 96, 57};
      int size = array.length;

      for (int i = 0 ;i< size-1; i++){


         int min = i;

         for (int j = i+1; j<size; j++){


            if (array[j] < array[min]){
            min = j;
      }
         }
         int temp = array[min];
         array[min] = array[i];
         array[i] = temp;
   }

      for (int i = 0 ;i< size; i++){


         System.out.print(" "+array[i]);
   }
   }  
}

-12-
-13-
Q7)Special Numbers

import java.util.*;

public class JavaHungry {

public static void main(String args[]) {


System.out.println("Enter any number : ");
Scanner scan = new Scanner(System.in);
int inputNumber = scan.nextInt();
boolean result = specialnumber(inputNumber);
if (result == true)
System.out.println(inputNumber + " is a Special number");
if (result == false)
System.out.println(inputNumber + " is NOT a Special number");
}

public static boolean specialnumber(int inputNumber) { 


 
  // Create a copy of the inputNumber
 int temp = inputNumber;
 
  // Initialize sumOfDigits of inputNumber
int sumOfDigits = 0; 
 
  /* Calculate the sum of factorial of
 digits */
 while (temp != 0) { 
  // Get the rightmost digit
 int currentDigit = temp % 10;
sumOfDigits = sumOfDigits + factorial(currentDigit);
temp = temp/10;

  /* If sumOfDigits is equal to inputNumber then
the number is Special, otherwise not */
 return (sumOfDigits == inputNumber);
}

public static int factorial(int number) {


if (number == 1 || number == 0)
return 1;
else
return (number * factorial(number -1));
}
}

-14-
Output Window-145

-15-

You might also like