0% found this document useful (0 votes)
18 views10 pages

Questions

The document contains a series of programming questions and corresponding Java code solutions that cover basic programming concepts such as variable declaration, input/output, conditional statements, loops, functions, and classes. Each question is designed to test a specific programming skill, and the solutions demonstrate how to implement these concepts in Java. The document serves as a practical guide for beginners to learn and practice Java programming.

Uploaded by

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

Questions

The document contains a series of programming questions and corresponding Java code solutions that cover basic programming concepts such as variable declaration, input/output, conditional statements, loops, functions, and classes. Each question is designed to test a specific programming skill, and the solutions demonstrate how to implement these concepts in Java. The document serves as a practical guide for beginners to learn and practice Java programming.

Uploaded by

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

Questions:

Q1: Create a variable age and store your age. Print it.

Q2: Store your name in a variable and print "Hello, [name]!"

Q3: Create two variables a = 10, b = 20, and print their sum.

Q4: Check if a number is positive, negative, or zero.

Q5: Check if someone is eligible to vote (age ≥ 18).

Q6: Given marks, print the grade: A (90+), B (75–89), C (50–74), Fail (<50).

Q7: Ask the user for their name and greet them.

Q8: Take two numbers as input and print the sum.

Q9: Ask the user for their age and check if they can drive (18+).

Q10: Print numbers from 1 to 10.

Q11: Print the sum of numbers from 1 to 100.

Q12: Print all even numbers from 1 to 50.

Q13: Print a square of * of size 5x5.

*****
*****
*****
*****
*****

Q14: Print a right triangle pattern of *:

*
**
***
****
*****

Q16: Create a function greet() that prints "Good Morning!".

Q17: Write a function that takes two numbers and returns their sum.

Q18: Write a function to check if a number is even or odd.

Q19: Create a class with a method sayHello() that prints "Hello from method!".

Q20: Write a method that returns the square of a number.

Q21: Write a method to print whether a number is positive or negative.

Q22: Ask the user to enter a number from 1 to 3:

1 → Print "One"

2 → Print "Two"
3 → Print "Three"

Q23: Ask for a day number (1 to 7) and print the day name using switch.

Q24: Print numbers from 1 to n → What's the time complexity? (O(n))

Q25: Print a 5x5 star pattern → How many total * will print? (25)

Q26: Ask for a number → print whether it’s even/odd → and print that many * using a
loop.
(User Input + Conditional + Loop + Pattern)

Q27: Create a menu using switch:

Say Hello

Print 1–10

Exit
(Switch + Loop + Function)

Answers:

1.)
import java.util.Scanner;

class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your age: ");
int a = sc.nextInt();
System.out.println(a);

}
}

2.)
import java.util.Scanner;

class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name: ");
String a = sc.nextLine();
System.out.println("Hello! " + a);

}
}

3.)
import java.util.Scanner;

class test {
public static void main(String[] args) {
int a = 10;
int b = 20;
int sum = a + b;
System.out.println(sum);
}
}

4.)
mport java.util.Scanner;

class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if(a == 0) {
System.out.println("Zero");
} else {
if(a > 0) {
System.out.println("Positive");
} else {
System.out.println("Negative");

}
}

}
}

5.)
import java.util.Scanner;

class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if(a == 0) {
System.out.println("Invalid");
} else {
if(a < 18) {
System.out.println("Not Eligible");
} else {
System.out.println("Eligible");
}
}

}
}

6.)
import java.util.Scanner;

class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if(a >= 90) {
System.out.println("A");
} else {
if(a >= 75) {
System.out.println("B");
} else {
if(a >= 50) {
System.out.println("C");
} else {
System.out.println("D");
}
}
}

}
}

7.)
import java.util.Scanner;

class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name: ");
String a = sc.nextLine();
System.out.println("Hello! " + a);

}
}

8.)
import java.util.Scanner;

class test {
public static void main(String[] args) {
int a = 10;
int b = 20;
int sum = a + b;
System.out.println(sum);
}
}

9.)
import java.util.Scanner;

class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your age: ");
int a = sc.nextInt();
if(a == 0) {
System.out.println("Invalid");
} else {
if(a < 18) {
System.out.println("Cannot Drive");
} else {
System.out.println("Can Drive");
}
}

}
}

10.)
import java.util.Scanner;
class test {
public static void main(String[] args) {
for(int i = 0; i <= 10; i++) {
System.out.println(i);
}

}
}

11.)
import java.util.Scanner;

class test {
public static void main(String[] args) {
int sum = 0;
for(int i = 0; i <= 100; i++) {
sum = sum + i;
System.out.println(sum);
}

}
}

12.)
import java.util.Scanner;

class test {
public static void main(String[] args) {
int sum = 0;
for(int i = 0; i <= 50; i++) {
if(i % 2 == 0) {
sum = sum + i;
System.out.println(sum);
}
}
}
}

13.)
import java.util.Scanner;

class test {
public static void main(String[] args) {
int sum = 0;
for(int i = 1; i <= 5; i++) {
for(int j = 1; j<=5; j++) {
System.out.print("*");
}
System.out.println();

}
}
}

14.)
import java.util.Scanner;
class test {
public static void main(String[] args) {
int sum = 0;
for(int i = 1; i <= 5; i++) {
for(int j = 1; j<=i; j++) {
System.out.print("*");
}
System.out.println();

}
}
}

16.)
import java.util.Scanner;

class test {
public static void greet(String[] args) {
System.out.println("Good Morning");
}
public static void main(String[] args) {
greet(args);

}
}

17.)
import java.util.Scanner;

class test {
public static void greet(int a, int b) {
int sum = a + b;
System.out.println(sum);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
greet(a, b);

}
}

18.)
import java.util.Scanner;

class test {
public static void greet(int a) {
if(a % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
greet(a);
}
}

19.)
import java.util.Scanner;

class HelloClass {
void sayHello() {
System.out.println("Hello from method!");
}
}

public class test {


public static void main(String[] args) {
HelloClass hello = new HelloClass();
hello.sayHello();
}
}

20.)
import java.util.Scanner;

public class test {


public static void main(String[] args) {
MathUtils math = new MathUtils();
int num = 5;
int result = math.square(num);
System.out.println("Square of " + num + " is: " + result);
}
}

21.)
import java.util.Scanner;

class NumberChecker {
void printSign(int number) {
if (number > 0) {
System.out.println(number + " is positive.");
} else if (number < 0) {
System.out.println(number + " is negative.");
} else {
System.out.println("The number is zero.");
}
}
}

public class test {


public static void main(String[] args) {
NumberChecker checker = new NumberChecker();
checker.printSign(10);
checker.printSign(-5);
checker.printSign(0);
}
}

22.)
import java.util.*;

class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if(a == 1) {
System.out.println("One");
}
if(a == 2) {
System.out.println("Two");
}
if(a == 3) {
System.out.println("Three");
} else {
System.out.println("Invalid");
}
}
}

23.)
import java.util.Scanner;

public class test {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter day number (1 to 7): ");
int day = scanner.nextInt();

switch (day) {
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
case 4:
System.out.println("Wednesday");
break;
case 5:
System.out.println("Thursday");
break;
case 6:
System.out.println("Friday");
break;
case 7:
System.out.println("Saturday");
break;
default:
System.out.println("Invalid day number!");
}
scanner.close();
}
}

24.)
import java.util.Scanner;

public class test {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter n: ");
int n = scanner.nextInt();

for (int i = 1; i <= n; i++) {


System.out.println(i);
}
}
}

Time Complexity: O(n).

25.)
import java.util.Scanner;

class test {
public static void main(String[] args) {
int sum = 0;
for(int i = 1; i <= 5; i++) {
for(int j = 1; j<=5; j++) {
System.out.print("*");
}
System.out.println();

}
}
}

No of Stars: 25

26.)
import java.util.Scanner;

public class test {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = scanner.nextInt();

if (n % 2 == 0) {
System.out.println(n + " is even.");
} else {
System.out.println(n + " is odd.");
}

for (int i = 0; i < n; i++) {


System.out.print("*");
}
System.out.println();

scanner.close();
}
}

27.)
import java.util.Scanner;

public class test {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Menu:");
System.out.println("1. Say Hello");
System.out.println("2. Print 1–10");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();

switch (choice) {
case 1:
System.out.println("Hello!");
break;
case 2:
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
break;
default:
System.out.println("Invalid choice.");
}
}
}

You might also like