0% found this document useful (0 votes)
2 views

2a.self-Practice_Introduction to Java - Questions (1)

The document outlines a self-practice training for Software Development Engineers (SDE) focusing on Java programming. It includes eight practice problems with sample inputs, outputs, and solutions, covering various topics such as conditional statements, calculations, and string manipulations. Each problem is categorized as 'Easy' and provides a Java code solution for learners to implement.

Uploaded by

k.s.yogeswar3
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

2a.self-Practice_Introduction to Java - Questions (1)

The document outlines a self-practice training for Software Development Engineers (SDE) focusing on Java programming. It includes eight practice problems with sample inputs, outputs, and solutions, covering various topics such as conditional statements, calculations, and string manipulations. Each problem is categorized as 'Easy' and provides a Java code solution for learners to implement.

Uploaded by

k.s.yogeswar3
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

SELF PRACTICE

SDE Readiness Training

Practice No. : 2a

Topic : Introduction to Java

Date : 13.02.2025

Solve the following problems

Question
Question Detail Level
No.

1 Determine whether to sleep in based on two conditions: Easy


'weekday' and 'vacation'. If it's not a weekday or if we're on
vacation, we sleep in. Write Java code to determine whether we
sleep in or not.

Sample Input: false false

Sample Output: true

Sample Input:true false

Sample Output: false

Sample Input: false true

Sample Output: true

Solution:

package self_practice;

import java.util.Scanner;

public class sleep {


public static boolean sleepIn(boolean weekday, boolean
vacation) {
return !weekday || vacation;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Is it a weekday? (true/false): ");
boolean weekday = scanner.nextBoolean();
System.out.print("Are you on vacation? (true/false): ");
boolean vacation = scanner.nextBoolean();
boolean canSleepIn = sleepIn(weekday, vacation);
System.out.println("Can we sleep in? " + canSleepIn);

scanner.close();
}
Sometimes later becomes never. DO IT NOW!
1
SELF PRACTICE
SDE Readiness Training
}
2 Determine whether we are in trouble based on the smiling status Easy
of two monkeys, 'aSmile' and 'bSmile'. We are in trouble if both
monkeys are smiling or if neither of them is smiling. Return true
if we are in trouble
Sample Input: false false

Sample Output: true

Sample Input: true true

Sample Output: true

Sample Input: false true

Sample Output: false

Solution:

package self_practice;

import java.util.Scanner;

public class monkeytrouble {


public static boolean areWeInTrouble(boolean aSmile, boolean
bSmile) {
return (aSmile && bSmile) || (!aSmile && !bSmile);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Is monkey A smiling? (true/false): ");
boolean aSmile = scanner.nextBoolean();
System.out.print("Is monkey B smiling? (true/false): ");
boolean bSmile = scanner.nextBoolean();
boolean inTrouble = areWeInTrouble(aSmile, bSmile);
System.out.println("Are we in trouble? " + inTrouble);
scanner.close();
}
}

3 We have a loud-talking parrot. The "hour" parameter is the Easy


current hour time in the range 0..23. We are in trouble if the
parrot is talking and the hour is before 7 or after 20. Print true if
we are in trouble.
Sample Input: 6 true

Sample Output: true

Sample Input: 7 true

Sometimes later becomes never. DO IT NOW!


2
SELF PRACTICE
SDE Readiness Training
Sample Output: false

Sample Input: 6 false

Sample Output: false

Solution:

package self_practice;

import java.util.Scanner;

public class parrottrouble {


public static boolean areWeInTrouble(boolean talking, int hour)
{
return talking && (hour < 7 || hour > 20);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the current hour (0-23): ");
int hour = scanner.nextInt();
System.out.print("Is the parrot talking? (true/false): ");
boolean talking = scanner.nextBoolean();
boolean inTrouble = areWeInTrouble(talking, hour);
System.out.println("Are we in trouble? " + inTrouble);
scanner.close();
}
}

4 We're hosting a party with tea and candy. The outcome of the Easy
party is encoded as follows: 0=bad, 1=good, or 2=great. A party
is considered good (1) if both tea and candy are at least 5. If
either tea or candy is at least double the amount of the other
one, the party is great (2). However, if either tea or candy is less
than 5, the party is always bad (0).
Sample Input: 6 8

Sample Output: 1

Sample Input: 3 8

Sample Output: 0

Sample Input: 20 6

Sample Output: 2

Solution:

package self_practice;

import java.util.Scanner;
Sometimes later becomes never. DO IT NOW!
3
SELF PRACTICE
SDE Readiness Training

public class partyoutcome {


public static int partyOutcome(int tea, int candy) {
if (tea < 5 || candy < 5) {
return 0;
}
if (tea >= 2 * candy || candy >= 2 * tea) {
return 2;
}
return 1;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the amount of tea: ");
int tea = scanner.nextInt();
System.out.print("Enter the amount of candy: ");
int candy = scanner.nextInt();
int outcome = partyOutcome(tea, candy);
System.out.println("Party Outcome: " + outcome);
scanner.close();
}
}

5 Given 2 non-negative ints, a and b, return their sum, so long as Easy


the sum has the same number of digits as a. If the sum has more
digits than a, just return a without b.

Sample Input: 2 3
Sample Output: 5
Sample Input: 8 3
Sample Output: 8
Solution:

package self_practice;

import java.util.Scanner;

public class sumCheck {


public static int sumLimit(int a, int b) {
int sum = a + b;
int aDigits = String.valueOf(a).length();
int sumDigits = String.valueOf(sum).length();
if (sumDigits > aDigits) {
return a;
}
return sum;
}
Sometimes later becomes never. DO IT NOW!
4
SELF PRACTICE
SDE Readiness Training

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number (a): ");
int a = scanner.nextInt();
System.out.print("Enter the second number (b): ");
int b = scanner.nextInt();
int result = sumLimit(a, b);
System.out.println("Result: " + result);
scanner.close();
}
}

Sometimes later becomes never. DO IT NOW!


5
SELF PRACTICE
SDE Readiness Training
6 Given an int n, return the string form of the number followed by Easy
"!". So the int 6 yields "6!". Except if the number is divisible by 3
use "Fizz" instead of the number, and if the number is divisible
by 5 use "Buzz", and if divisible by both 3 and 5, use "FizzBuzz".
Note: the % "mod" operator computes the remainder after
division, so 23 % 10 yields 3. What will the remainder be when
one number divides evenly into another?

Solution:

package self_practice;

import java.util.Scanner;

public class fizzBuzzString {


public static String fizzBuzz(int n) {
if (n % 3 == 0 && n % 5 == 0) {
return "FizzBuzz!";
} else if (n % 3 == 0) {
return "Fizz!";
} else if (n % 5 == 0) {
return "Buzz!";
} else {
return n + "!";
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = scanner.nextInt();
String result = fizzBuzz(n);
System.out.println("Result: " + result);
scanner.close();
}
}

Sometimes later becomes never. DO IT NOW!


6
SELF PRACTICE
SDE Readiness Training
7 Write a Java program to reverse a 3-digit number. Easy

Solution:

package self_practice;

import java.util.Scanner;

public class reverseThreeDigits {


public static int reverseNumber(int n) {
if (n < 100 || n > 999) {
System.out.println("Error: Please enter a 3-digit number.");
return -1;
}
int last = n % 10;
int middle = (n / 10) % 10;
int first = n / 100;
return (last * 100) + (middle * 10) + first;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a 3-digit number: ");
int n = scanner.nextInt();
int reversed = reverseNumber(n);
if (reversed != -1) {
System.out.println("Reversed Number: " + reversed);
}
scanner.close();
}
}

8 Write a Java program to calculate Net Salary. User must input Easy
Basic Salary and Output should be net salary calculated based on
the following allowances:
Allowances:
DA = 70% of Basic Salary
HRA = 7% of Basic Salary
MA = 2% of Basic Salary
TA = 4% of Basic Salary
Deduction:
PF = 12% of Basic Salary
Income/professional tax = User Input (e.g., 500)
Net Salary = Basic Salary + Allowances – Deduction

Solution:

package self_practice;

import java.util.Scanner;
Sometimes later becomes never. DO IT NOW!
7
SELF PRACTICE
SDE Readiness Training

public class netSalaryCalculator {


public static double calculateNetSalary(double basicSalary,
double tax) {
double da = 0.70 * basicSalary;
double hra = 0.07 * basicSalary;
double ma = 0.02 * basicSalary;
double ta = 0.04 * basicSalary;
double pf = 0.12 * basicSalary;
double netSalary = basicSalary + (da + hra + ma + ta) - (pf
+ tax);
return netSalary;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Basic Salary: ");
double basicSalary = scanner.nextDouble();
System.out.print("Enter Income/Professional Tax: ");
double tax = scanner.nextDouble();
double netSalary = calculateNetSalary(basicSalary, tax);
System.out.println("Net Salary: " + netSalary);
scanner.close();
}
}

Sometimes later becomes never. DO IT NOW!


8

You might also like