Unit 3 – Java Programming
1. Give the output of the following Java code segment (Factorial code).
int Num=4;
int Fact=1;
for (int i=1;i<=Num;i++)
{
Fact*=i;
System.out.println(Fact);
}
Answer:The program calculates the factorial of a number using a loop. If input n = 5, the
output will be 120.
Explanation:
1 × 2 × 3 × 4 × 5 = 120.
2.Correct the syntax errors in the given Java code (while loop example). Rewrite the
following code after correcting the syntax errors, underline each correction made:
int number= = 1;
while (number c = 5 );
{
System.out.print("Square of " + number);
System.out.println("=", number* number);
Number += 1 ;
}
3.Convert the following for loop to an equivalent while loop.
int x = 4 , Y = 3;
int pow = 1;
for (int i = 1, i<=Y;i++)
Pow* = X ;
System.out.println(Pow);
Answer.int x = 4, Y = 3;
int pow = 1;
int i = 1; // Initialization of loop counter
while (i <= Y) { // Loop condition
pow *= x; // Loop body
i++; // Update statement
System.out.println(pow);
4. Give the output of following code:String myString = "Welcome to Java";
a)System.out.println (myString.concat("Session"));
b)System.out.println (myString.length());
c)System.out.println (myString.replace("to","2"));
d) Expand the term OOP.
Answer.a)Welcome to JavaSession
b)15
c)Welcome 2 Java
d) object oriented programming
5.Consider the following code and answer the following questions:
static int MyMethod(int N)
{
return (N * N);
}
public static void main(String[] args)
{
System.out.println(MyMethod(7));
}
i) Name the user-defined method.
ii) What will be the output on executing the above code?
iii) Explain exception handling.
Answer.a)The user-defined method is MyMethod.
b)The method MyMethod calculates the square of its input. When called with the argument
7, it returns 7 * 7, which is 49.
c) Learn from book
6.a)What is an array in Java? Write a short code example.
b)Write Java code to do the following:
i) Create an array Marks that stores values 78,65,85,91,82
ii) Display the average stored in the array Marks.
iii) To print all the values of the array Marks using a loop.
Answer:
An array is a collection of elements of the same data type stored in contiguous memory.
Example:
int arr[] = {1,2,3,4,5};
for(int i : arr)
System.out.print(i + ' ');
ii)public class ArrayOperations {
public static void main(String[] args) {
// i) Create an array Marks that stores values 78, 65, 85, 91, 82
int[] marks = {78, 65, 85, 91, 82};
// ii) To print all the values of the array Marks using a loop
System.out.println("Marks in the array:");
for (int i = 0; i < marks.length; i++) {
System.out.println(marks[i]);
// iii) Display the average stored in the array Marks.
double sum = 0;
for (int mark : marks) {
sum += mark;
double average = sum / marks.length;
System.out.println("Average of the marks: " + average);
7.Write a Java program to find factorial of a number using loop.
class Factorial {
public static void main(String args[]) {
int n = 5, fact = 1;
for(int i = 1; i <= n; i++) {
fact = fact * i;
}
System.out.println("Factorial of " + n + " is: " + fact);
}
}
8.Write a Java program to check whether a number is prime or not.
class PrimeCheck {
public static void main(String args[]) {
int num = 7;
boolean isPrime = true;
for(int i = 2; i <= num/2; i++) {
if(num % i == 0) {
isPrime = false;
break;
}
}
if(isPrime)
System.out.println(num + " is a Prime Number");
else
System.out.println(num + " is not a Prime Number");
}
}
9.Write a Java program to print Fibonacci series.
// Program to print Fibonacci Series
class FibonacciSeries {
public static void main(String args[]) {
int n1 = 0, n2 = 1, n3, i, count = 10;
System.out.print("Fibonacci Series: " + n1 + " " + n2);
for (i = 2; i < count; i++) {
n3 = n1 + n2;
System.out.print(" " + n3);
n1 = n2;
n2 = n3;
}
}
}
10.Write a Java program to check whether a string is palindrome or not.
class Palindrome {
public static void main(String args[]) {
String str = "MADAM";
String rev = "";
for(int i = str.length() - 1; i >= 0; i--) {
rev = rev + str.charAt(i);
}
if(str.equals(rev))
System.out.println(str + " is Palindrome");
else
System.out.println(str + " is not Palindrome");
}
}
11.Write a Java program to find the largest of three numbers.
class Largest {
public static void main(String args[]) {
int a = 10, b = 25, c = 15;
if(a > b && a > c)
System.out.println(a + " is largest");
else if(b > c)
System.out.println(b + " is largest");
else
System.out.println(c + " is largest");
}
}
12.Write a Java program to calculate sum of digits of a number.
class SumOfDigits {
public static void main(String args[]) {
int num = 1234, sum = 0;
while(num != 0) {
sum = sum + num % 10;
num = num / 10;
}
System.out.println("Sum of digits = " + sum);
}
}
13.Write a Java program to swap two numbers without using a third variable.
class SwapNumbers {
public static void main(String args[]) {
int a = 5, b = 10;
System.out.println("Before swap: a = " + a + ", b = " + b);
a = a + b;
b = a - b;
a = a - b;
System.out.println("After swap: a = " + a + ", b = " + b);
}
}
Basic Questions
14. What is Java?
15.. Write any five features of Java.
16. What is JVM, JRE, and JDK?
17. What is the difference between compiler and interpreter?
18. What do you mean by platform independence in Java?
19. What is bytecode?
20. What are variables and data types in Java?
21. What is the difference between primitive and non-primitive data types?
22. What are operators in Java? List types of operators with examples.
23. Define identifier and literals.
Control Statements**
1. What are conditional statements in Java?
2. Explain `if`, `if-else`, and `nested if` statements with examples.
3. What is a `switch` statement? Write its syntax.
4. What are loops? Explain `for`, `while`, and `do-while` loops.
5. What is the difference between `break` and `continue` statements?
6. What is the difference between entry-controlled and exit-controlled loops?
**3. Arrays and Strings**
1. What is an array?
2. How do you declare and initialize an array in Java?
3. What is the difference between one-dimensional and two-dimensional arrays?
4. What is a string in Java?
5. Differentiate between `String` and `StringBuffer` class.
6. Write two methods of `String` class with their uses.
*4. Functions / Methods**
1. What is a method in Java?
2. What is the difference between call by value and call by reference?
3. What is method overloading?
4. Why do we use `return` statement?
5. What is recursion? Give an example.
5. Classes and Objects**
1. Define class and object.
2. What is the difference between class and object?
3. What is a constructor?
4. Differentiate between default constructor and parameterized constructor.
5. What is the use of the `this` keyword?
6. What is encapsulation?
7. What is inheritance? List its types.
8. What is polymorphism?
9. Explain the concept of data abstraction.
10. What are access specifiers? List them.
*6. Exception Handling**
1. What is exception handling?
2. What is an exception?
3. List and explain any two types of exceptions.
4. What is the difference between checked and unchecked exceptions?
5. Explain the use of `try`, `catch`, `finally`, `throw`, and `throws`.
6. Write a short note on runtime errors in Java.
7. File Handling (if included in syllabus)**
1. What is file handling?
2. How to open and close a file in Java?
3. What are input and output streams?
4. What is the difference between `FileReader` and `BufferedReader`?
5. Write a Java statement to read a line from a text file.
*8. General / Short Questions**
1. Who developed Java and in which year?
2. What is the extension of Java source code file?
3. What is the purpose of `main()` method?
4. What is the default value of `int` and `boolean` in Java?
5. Define keywords and identifiers with examples.