Untitled Document
Untitled Document
Used to select one of many code blocks to be executed based on a variable's value. It's an
alternative to using multiple if-else statements.
Jump Statements:
break statement: Terminates the loop or switch statement it's in and transfers control to the
statement immediately following the loop or switch.
continue statement: Skips the current iteration of a loop and continues with the next iteration.
3)How do you achieve multiple inheritance in Java , explain in detail.
Ans.In Java, multiple inheritance, i.e., inheriting from more than one class, is not directly
supported due to concerns about ambiguity and complexity in resolving conflicts that may arise
when methods or attributes with the same name exist in multiple parent classes.
Instead, Java allows for multiple inheritance through interfaces. An interface defines a contract
that implementing classes must follow but doesn't provide any method implementations. A class
can implement multiple interfaces, effectively inheriting multiple sets of method signatures
without the conflicts of multiple inheritance.
-Sample code.
interface A {
void methodA();
}
interface B {
void methodB();
}
// Calculate GCD
int gcd = calculateGCD(number1, number2);
scanner.close();
}
}
20)Write a sample java program to find the factorial of a number.
Ans:
import java.util.Scanner;
// Input number
System.out.print("Enter a number to calculate its factorial: ");
int number = scanner.nextInt();
// Calculate factorial
int factorial = calculateFactorial(number);
scanner.close();
}
}
21)Write a sample java program to find the prime numbers.
Ans:
import java.util.Scanner;
// Input limit
System.out.print("Enter the limit to find prime numbers up to that limit: ");
int limit = scanner.nextInt();
scanner.close();
}
}
22)Write a sample java program to find the armstrong numbers.
Ans:
import java.util.Scanner;
public class ArmstrongNumberFinder {
// Method to check if a number is an Armstrong number
public static boolean isArmstrong(int num) {
int originalNum, remainder, result = 0, n = 0;
originalNum = num;
originalNum = num;
// Calculate result
while (originalNum != 0) {
remainder = originalNum % 10;
result += Math.pow(remainder, n);
originalNum /= 10;
}
System.out.println("Armstrong numbers between " + from + " and " + to + " are:");
scanner.close();
}
}
23)Write a sample java program to find the even or odd numbers
Ans:
import java.util.Scanner;
// Input number
System.out.print("Enter a number to check if it's even or odd: ");
int number = scanner.nextInt();
scanner.close();
}
}
26)Explain about Concepts of exception handling?
Ans:
-Exception: An exception is an event that occurs during the execution of a program, disrupting
the normal flow. It can be caused by various factors like incorrect input, hardware failures, or
programming errors.
-Try-Catch block: This block is used to handle exceptions. The code that might throw an
exception is placed within the try block. If an exception occurs, it's caught by the catch block,
allowing the program to respond to the exceptional condition.
-Throwing exceptions: Exceptions can be manually thrown using the throw keyword. This is
useful when a specific condition is met and the programmer wants to explicitly signal an
exceptional condition.
- Finally block: This block follows a try-catch block and contains code that needs to be
executed regardless of whether an exception occurred or not. It's commonly used to release
resources or perform cleanup operations.
27)Explain why Java is Machine Independent.
Ans:
1.Java Virtual Machine (JVM): Java code is compiled into bytecode, which isn't tied to any
specific hardware or operating bytecode is executed by the JVM, which is platform-specific.
JVMs are available for various platforms, and they interpret bytecode into machine code on the
fly, allowing Java programs to run on different systems without modification.
2.Write Once, Run Anywhere (WORA): Java follows the principle of WORA. Developers can
write Java code on one platform and expect it to run on any other platform with a compatible
JVM, without needing to recompile the code.
3.Abstraction of Hardware: Java abstracts many hardware-specific functionalities through its
libraries and APIs. For instance, file handling, networking, and graphical user interfaces are
managed by Java libraries, shielding the developer from underlying hardware intricacies.
4.No Native Code: Unlike some other programming languages, Java doesn't generate native
code specific to a particular platform during compilation. Instead, it generates bytecode, which is
platform-independent.
5.Bytecode Verification: Before executing bytecode, the JVM verifies it to ensure security and
stability. This verification process adds another layer of independence from the underlying
hardware or OS.
Pass-by-value: Primitive data types (like int, float, etc.) are passed by value, which means a
copy of the actual value is passed to the method. Changes made to the parameter inside the
method do not affect the original variable outside the method.
Pass- by reference(aliasing): Changes made to formal parameter do get transmitted back to
the caller through parameter passing. Any changes to the formal parameter are reflected in the
actual parameter in the calling environment as formal parameter receives a reference (pointer)
to the actual data. This method is also called as call by reference. This method is efficient in
both time and space.
Abstract methods are invoked or executed through polymorphism. When an object of the
subclass is created, and one of its methods (which overrides the abstract method) is called, the
overridden method in the subclass gets executed. This allows the abstraction defined in the
abstract class to be realized in the concrete subclass implementation.
36) Discuss the uses of final keyword?
Ans:
Constants: When applied to a variable, final makes it a constant, meaning its value cannot be
changed after initialization.
Method: When applied to a method, final prevents the method from being overridden in any
subclass, providing a fixed implementation.
Class: Declaring a class as final prevents other classes from extending it, making it impossible
to create subclasses.
Performance optimization: In some cases, the final keyword can assist the compiler in
optimization, allowing for potentially faster execution of code by enabling certain optimizations.
37) Elaborate who polymorphism is achieved in java.
Polymorphism is one of the main aspects of Object-Oriented Programming(OOP). The word
polymorphism can be broken down into Poly and morphs, as “Poly” means many and “Morphs”
means forms. In simple words, we can say that ability of a message to be represented in many
forms.Polymorphism in Java can be achieved in two ways i.e., method overloading and method
overriding.
Polymorphism in Java is mainly divided into two types.
Compile-time polymorphism
Runtime polymorphism
class Student {
String name;
int age;
student.displayInfo(student.name);
student.displayInfo(student.age);
student.displayInfo(student.name, student.age);
}
}
45,46,47Q)
40Q
34Q)