Question 1 : Write a Java program to get a number from the user and print whether it is positive or negative.
Solution code :
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
if (x > 0) {
System.out.println("x is greater than 0");
} else {
System.out.println("x is less than or equal 0");
}
}
}
Question 2 : Finish the following code so that it prints You have a fever if your temperature is above 100 and otherwise prints You don't have a fever.
public class Solution {
public static void main(String[] args) {
double temp = 103.5;
}
}
Solution Code :
public class Solution{
public static void main(String[] args) {
double temp = 103.5;
if (temp > 100) {
System.out.println("You have a fever");
} else {
System.out.println("You don't have a fever");
}
}
}
Question 3 : Write a Java program to input week number(1-7) and print day of week name using switch case.
Solution Code :
import java.util.*;
public class Solution {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter week number(1-7): ");
int week = sc.nextInt();
switch(week) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid input! Please enter week number between1-7.");
}
}
}
Question 4 : What will be the value of x & y in the following program:
public class Solution {
public static void main(String args[]) {
int a = 63, b = 36;
boolean x = (a < b ) ? true : false;
int y= (a > b ) ? a : b;
}
}
Solution :
Value of x = false & y = 63
Question 5 : Write a Java program that takes a year from the user and print whether that year is a leap year or not.
Solution Code :
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Input the year: ");
int year = sc.nextInt();
boolean x = (year % 4) == 0;
boolean y = (year % 100) != 0;
boolean z = ((year % 100 == 0) && (year % 400 == 0));
if (x && (y || z)) {
System.out.println(year + " is a leap year");
} else {
System.out.println(year + " is not a leap year");
}
}
}
Thanks for reading...........

0 Comments