9th class pratice 1
🧠 Section A: Multiple
Choice Questions (1 mark each × 5 = 5 marks)
Q1. Which of the following is a valid variable name
in Java?
a) 2number
b) myNumber
c) my number
d) int
Answer: b) myNumber
Q2. Which of the following data types can store a
decimal value?
a) int
b) char
c) double
d) boolean
Answer: c) double
Q3. The Scanner class is defined in which package?
a) java.util
b) java.io
c) java.lang
d) java.text
Answer: a) java.util
Q4. What is the result of (int) 9.7 in Java?
a) 9.0
b) 9
c) 10
d) Error
Answer: b) 9
Q5. Which method of the Math class returns the square
root of a number?
a) Math.pow()
b) Math.sqrt()
c) Math.abs()
d) Math.round()
Answer: b) Math.sqrt()
✍️ Section B: Short Answer
Questions (2 marks each × 5 = 10 marks)
Q1. Define a variable. Give one example.
Answer: A variable is a named memory location used to store a value.
Example: int age = 15;
Q2. Differentiate between int and double data types.
Answer:
- int
stores whole numbers (e.g., 5, -10).
- double
stores decimal numbers (e.g., 5.5, -3.14).
Q3. Write the steps to use the Scanner class for
input.
Answer:
- Import
the class: import java.util.Scanner;
- Create
object: Scanner sc = new Scanner(System.in);
- Take
input: int num = sc.nextInt();
Q4. What is type casting? Give an example.
Answer: Converting one data type into another.
Example:
double a = 5.7;
int b = (int)a; // b
= 5
Q5. What will be the output of Math.max(8, 12)?
Answer: 12
💻 Section C: Long Answer
Questions (15 marks each × 4 = 60 marks)
Q1. Write a Java program to input two numbers using
the Scanner class and display their sum, difference, and product.
Answer:
import java.util.*;
class Calculate {
public static void
main(String[] args) {
Scanner sc =
new Scanner(System.in);
System.out.print("Enter first number: ");
int a =
sc.nextInt();
System.out.print("Enter second number: ");
int b =
sc.nextInt();
System.out.println("Sum = " + (a + b));
System.out.println("Difference = " + (a - b));
System.out.println("Product = " + (a * b));
}
}
Q2. Write a Java program to input a floating-point
number and display its:
(a) Rounded value
(b) Square root
(c) Power of 3 using Math class.
Answer:
import java.util.*;
class MathExample {
public static void
main(String[] args) {
Scanner sc =
new Scanner(System.in);
System.out.print("Enter a number: ");
double num =
sc.nextDouble();
System.out.println("Rounded value: " + Math.round(num));
System.out.println("Square root: " + Math.sqrt(num));
System.out.println("Cube: " + Math.pow(num, 3));
}
}
Q3. Write a Java program to demonstrate type casting
from double to int and int to double.
Answer:
class TypeCastingDemo {
public static void
main(String[] args) {
double d =
9.75;
int i =
(int)d; // Explicit casting
int x = 10;
double y =
x; // Implicit casting
System.out.println("Double to int: " + i);
System.out.println("Int to double: " + y);
}
}
Q4. Write a Java program to input radius and find the
area and circumference of a circle using the Math class.
Answer:
import java.util.*;
class Circle {
public static void
main(String[] args) {
Scanner sc =
new Scanner(System.in);
System.out.print("Enter radius: ");
double r =
sc.nextDouble();
double area =
Math.PI * Math.pow(r, 2);
double
circumference = 2 * Math.PI * r;
System.out.println("Area = " + area);
System.out.println("Circumference = " + circumference);
}
}
Comments
Post a Comment