Java Assignment
Home Task
Name: Muhammad Faizan
Dept: DAE SWT 1st Year
Roll No: FD25501006
Assignment
1. Write a Java program to take two integer numbers
from the user and display their sum.
Ans: import [Link];
public class Practice {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the num1: ");
int num1 = [Link]();
[Link]("Enter the num2: ");
int num2 = [Link]();
int sum = num1 + num2;
[Link]("Sum: " + sum);
[Link]();
}
}
2. Write a Java program that multiplies a float number
and an integer number and prints the result.
Ans: import [Link];
public class Practice {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a float num: ");
float num1 = [Link]();
[Link]("Enter an integer: ");
int num2 = [Link]();
float prod = num1 * num2;
[Link]("Product: " + prod);
[Link]();
}
}
3. Write a Java program to input your name using
Scanner and display a welcome message?
Ans: import [Link];
public class Practice {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Your Name: ");
String name = [Link]();
[Link]("Good Morning, " + name + ",
How are you!");
[Link]();
}
}
4. Write a Java program that stores a character in a
variable and prints it twice on separate lines.
Ans: import [Link];
public class Practice {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter A Character: ");
char name = [Link]().charAt(0);
[Link](name);
[Link](name);
[Link]();
}
}
5. Write a Java program to take your age as input and
print?
Ans: import [Link];
public class Practice {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your Age: ");
int age = [Link]();
[Link]("Your are " + age + " years
old");
[Link]();
}
}
6. What is the difference between int, float, and char
data types?
Ans: Below are the differences stated:
int: Stores whole numbers (e.g., -5, 0, 42). Size: 4 bytes.
Range: -2,147,483,648 to 2,147,483,647.
float: Stores decimal numbers with single precision (e.g.,
3.14, -0.001). Size: 4 bytes. Less precise than double.
char: Stores a single character (e.g., 'A', '5', '@'). Size: 2
bytes (Unicode). Used for text-related data.
7. What is the purpose of the Scanner class?
Ans: The Scanner class in Java is used to read input from
sources like the keyboard. It helps you get data from
users by providing simple methods to read numbers (int,
float), words, or lines of text. For example, it can read a
number you type with nextInt() or a word with next(). It
makes handling user input easy.
8. What method is used to take integer input from the
user?
Ans: The nextInt() method of the Scanner class is used to read
an integer input from the user.
9. How do you print text in Java? Write syntax.
Ans: In Java, you can print text to the console using
[Link]("Your text here"); , which outputs the
text and adds a new line. For example,
[Link]("Hello"); prints "Hello" followed by a
new line. If you want to print text without a new line, use
[Link]("Your text here"); . Both methods are
simple and commonly used for displaying output in
console-based programs.
10. What is the use of the '+' operator in Java?
Ans: In Java, the + operator has two main uses:
1. Addition: Adds two numbers (e.g., int sum = 5 + 3; //
sum is 8).
2. String Concatenation: Combines strings or a string with
other data types (e.g., String text = "Hello " + "World"; //
text is "Hello World" or "Number: " + 5; // "Number: 5").
11. Write a program to input your name and print a
welcome message.
Ans: import [Link];
public class Practice {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Your Name: ");
String name = [Link]();
[Link]("Good Morning, " + name + ",
How are you!");
[Link]();
}
}
12. Explain the difference between '=' and '==' operators.
Ans: Given Below are the differences:
= (Assignment Operator): Assigns a value to a variable.
Example: int x = 5; sets x to 5.
== (Equality Operator): Checks if two values are equal,
returning true or false. Example: x == 5 checks if x
equals 5.
Main Difference:
o = changes a variable’s value.
o == compares values without changing them.
Usage: Use = to store data, == to test equality.
13. What will happen if you divide two integers like 5 / 2?
Ans: When you divide two integers in Java, like 5 / 2, the
result is an integer because Java performs integer
division. The quotient is calculated, and any decimal part
is discarded. So, 5 / 2 equals 2 (since 5 ÷ 2 = 2.5, and the
.5 is truncated). To get a decimal result, at least one
operand must be a floating-point type (e.g., 5.0 / 2 yields
2.5).
14. What does '%' operator return?
Ans: The % operator, it's also called the modulus operator, in
its Java returns the remainder of dividing one number by
another. For example, 5 % 2 returns 1 because 5 ÷ 2
leaves a remainder of 1. It works with integers and is
useful for tasks like checking if a number is even or odd.
If the divisor is 0, it throws an ArithmeticException.
15. Write a short note on variables in Java.
Ans: In Java, variables are used to store data that can be used
and changed in a program. They act as named containers that
hold values of a specific data type, such as int, float, char, or
String. Each variable must be declared with a data type and a
name (e.g., int count = 10;), and can be assigned or reassigned
values using the = operator. They must follow naming rules:
start with a letter, underscore, or dollar sign, and cannot be
reserved keywords. Properly using variables ensures efficient
data management and program functionality.