0% found this document useful (0 votes)
4 views3 pages

Java Programming Concepts and Exercises

Uploaded by

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

Java Programming Concepts and Exercises

Uploaded by

khasimshaik00001
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Unit – I

2 – mark Questions
1. List down the features of Java.
2. Define Inheritance and Polymorphism.
3. Define Abstraction and Encapsulation.
4. Why is Java called Platform Independent?
5. How do escape sequences improve readability in Java programs? Give one example.
6. Why do we use command-line arguments in Java programs? Provide a small scenario.
7. What happens if you assign a double value to an int variable without type casting?
Justify.
8. Differentiate between static and instance variables with an example use case. Why is type
casting necessary in Java? Give one practical case where it is useful.
9. Predict the output of:
int a = 7, b = 2;
[Link](a / b);
10. Predict the output of the following and justify:
int i = 1;
while(i <= 5) {
if(i % 2 == 0)
[Link](i + " ");
i++;
}
11. Predict the output of the following and justify:
for(int i = 1; i <= 5; i++) {
if(i == 3) continue;
[Link](i + " ");
}
12. Arrange the following operators in order of precedence: +, *, =, ++.
13. Explain the difference between relational operators and logical operators in Java with an
example.
14. If 𝑏𝑜𝑜𝑙𝑒𝑎𝑛 𝑎 = 𝑡𝑟𝑢𝑒, 𝑏 = 𝑓𝑎𝑙𝑠𝑒; what is the output of 𝑎 && 𝑏 || 𝑎? Explain.
15. Differentiate between while and do-while loop with a simple example.
16. When would you prefer using a switch statement instead of multiple if-else statements?
17. Consider the loop:
for(int i=1; i<=5; i++) {
if(i==3) break;
[Link](i);
}
What will be the output? Why?
18. Write a small Java snippet using a nested for loop to print:
*
**
***
19. Why is the enhanced for-each loop not suitable for modifying array elements directly?

Essay Questions
1. Explain the features of Java.
2. Write a Java program that accepts two command-line arguments (integers) and prints
their sum. Explain how arguments are passed.
3. Explain how operator precedence affects the evaluation of the following:
int x = 10, y = 20, z = 5;
int result1 = x + y * z;
int result2 = (x + y) * z;
int result3 = x + (y * z);
Explain the outputs with reference to precedence and associativity rules.
4. Discuss the importance of programming style in Java. Write two examples of poorly
written but correct code, and then rewrite them with proper style.
5. What is type casting? Write a Java program to demonstrate both implicit and explicit type
casting.
6. Explain with examples the difference between static and non-static methods. Write a
program that demonstrates both.
7. Write a Java program that checks whether a given number is both even and greater than
10.
8. Explain operator precedence with an example where the output changes based on the use
of parentheses.
9. Write a Java program to check whether the given year is a Leap year or not. Explain the
decision-making process.
10. Compare switch and if-else statements by writing a program to print the day of the week
(1 = Monday … 7 = Sunday) using both approaches. Which one is clearer and why?

11. Explain the Core Principles of Java.


12. Java enforces the use of classes for even the simplest program. Discuss the significance
of this design choice compared to procedural languages like C. Support your answer with
an example program that prints the first 5 prime numbers.
13. Explain Java’s data types in detail.
14. Explain for and for..each loops. Design a program to generate the following pattern
1
12
123
1234
12345

15. Explain the difference between local variables, instance variables, and static variables in
Java. Give one real-world analogy for each.
16. Compare break, continue, and return statements in Java. How do they differ in control
flow? Explain with an example.
17. Write a Java program that prints numbers from 1 to 20 and Skips even numbers, stops
printing when the number reaches 17, exit the method when the number 10 is
encountered.

You might also like