0% found this document useful (0 votes)
8 views4 pages

Basic Java Interview Notes Dnyx

The document provides comprehensive Java programs for calculating simple and compound interest, finding the perimeter of a rectangle, and generating various patterns using nested loops. Each program includes Java code, explanations, and interview questions with answers related to the concepts. Additionally, it outlines the time and space complexities for each program.

Uploaded by

officialmaha204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Basic Java Interview Notes Dnyx

The document provides comprehensive Java programs for calculating simple and compound interest, finding the perimeter of a rectangle, and generating various patterns using nested loops. Each program includes Java code, explanations, and interview questions with answers related to the concepts. Additionally, it outlines the time and space complexities for each program.

Uploaded by

officialmaha204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Basic Java Interview Notes

Prepared by Dnyx Business Solutions Pvt. Ltd.

Comprehensive Java Programs with Explanations, Code, and Interview Q&A


1. Calculate Simple Interest
Formula: (Principal * Rate * Time) / 100

Java Code:
import java.util.Scanner;
public class SimpleInterest {
public static double calculate(double p, double r, double t) {
return (p * r * t) / 100.0;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double p = sc.nextDouble(), r = sc.nextDouble(), t =
sc.nextDouble();
System.out.printf("Simple Interest = %.2f%n", calculate(p, r,
t));
}
}

Interview Q&A:
Q: What is the difference between rate and time?
A: Rate is annual interest percentage, time is the period in years.

Time Complexity: O(1) | Space Complexity: O(1)

2. Calculate Compound Interest


Formula: A = P * (1 + r/(100*n))^(n*t); CI = A - P

Java Code:
import java.util.Scanner;
public class CompoundInterest {
public static double calculate(double p, double r, double t, int n)
{
double rate = r / 100.0;
double amount = p * Math.pow(1 + rate / n, n * t);
return amount - p;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double p = sc.nextDouble(), r = sc.nextDouble(), t =
sc.nextDouble();
int n = sc.nextInt();
System.out.printf("Compound Interest = %.2f%n", calculate(p, r,
t, n));
}
}

Interview Q&A:
Q: When is compound interest higher than simple interest?
A: When compounding frequency is greater than once per year.
Time Complexity: O(1) | Space Complexity: O(1)

3. Find the Perimeter of a Rectangle


Formula: 2 * (Length + Width)

Java Code:
import java.util.Scanner;
public class RectanglePerimeter {
public static double perimeter(double l, double w) {
return 2 * (l + w);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double l = sc.nextDouble(), w = sc.nextDouble();
System.out.printf("Perimeter = %.2f%n", perimeter(l, w));
}
}

Interview Q&A:
Q: What if one of the sides is negative?
A: We should validate input; sides of a rectangle cannot be negative.

Time Complexity: O(1) | Space Complexity: O(1)

4. Java Pattern Programs


This section includes all basic star and number pattern programs. Each uses nested loops to
control rows and columns.

import java.util.Scanner;
public class Patterns {
public static void rightTriangle(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) System.out.print("*");
System.out.println();
}
}
public static void pyramid(int n) {
for (int i = 1; i <= n; i++) {
for (int s = 0; s < n - i; s++) System.out.print(" ");
for (int j = 0; j < 2 * i - 1; j++) System.out.print("*");
System.out.println();
}
}
public static void diamond(int n) {
for (int i = 1; i <= n; i++) {
for (int s = 0; s < n - i; s++) System.out.print(" ");
for (int j = 0; j < 2 * i - 1; j++) System.out.print("*");
System.out.println();
}
for (int i = n - 1; i >= 1; i--) {
for (int s = 0; s < n - i; s++) System.out.print(" ");
for (int j = 0; j < 2 * i - 1; j++) System.out.print("*");
System.out.println();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
rightTriangle(n);
pyramid(n);
diamond(n);
}
}
Time Complexity: O(n²) | Space Complexity: O(1)

Q: What is the time complexity of most pattern programs?


A: O(n²), since each pattern prints roughly n² characters.

You might also like