CH 5-6 Questions
CH 5-6 Questions
Instructions:
System.out.println(Math.max(45, 89));
System.out.println(Math.abs(-15));
Question 2: [10 Marks]
(Output-Based Questions)
Write the output of the following code snippets:
a)
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
System.out.println(Math.pow(x, y));
}
}
Input: 2 3
b)
c)
double a = 7.4;
System.out.println(Math.round(a));
System.out.println(Math.floor(a));
System.out.println(Math.ceil(a));
d)
System.out.println("Java" + 10 + 20);
e)
String s = "Hello";
System.out.println(s.length());
Question 3: [15 Marks]
(Programming Questions)
Write complete programs for the following:
a) Write a program to input a user's name, age, and percentage marks. Display them in a
well-formatted output. [5]
c) Write a program to generate a random number between 1 and 100, and display it with a
message:
"Your lucky number is: ___" [5]
b) What will happen if we call nextLine() immediately after nextInt()? How do we fix it?
d) Rewrite this expression so that it rounds up the number 5.2 to the next integer:
Math.floor(5.2);
e) Write the import statement needed to use both Scanner and Math methods.
Question 5: [10 Marks]
(Application-Oriented Questions)
a) Write a program to input a temperature in Celsius and convert it to Fahrenheit. Use input and
Math functions appropriately. Formula: F = (C * 9/5) + 32 [5]
b) Write a program to input a number and check whether it is positive, negative or zero. Use
Math.abs() for displaying the absolute value in the output. [5]