[Go to site: main page, start]

0% found this document useful (0 votes)
7 views12 pages

Java Programming Examples and Outputs

The document contains a series of Java programming exercises, each with a specific task, code implementation, and output example. Topics include pattern printing, finding the greatest number, calculating averages, using loops, and demonstrating object-oriented programming concepts such as inheritance, method overloading, and exception handling. Each question is presented with a clear structure, showcasing the problem statement, corresponding code, and expected output.
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)
7 views12 pages

Java Programming Examples and Outputs

The document contains a series of Java programming exercises, each with a specific task, code implementation, and output example. Topics include pattern printing, finding the greatest number, calculating averages, using loops, and demonstrating object-oriented programming concepts such as inheritance, method overloading, and exception handling. Each question is presented with a clear structure, showcasing the problem statement, corresponding code, and expected output.
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

Java Programs: Questions, Programs and Outputs

Q1. Write a program to implement following pattern * ** *** **** *****


Program:
class Pattern1 {
public static void main(String[] args) {
for(int i=1; i<=5; i++){
for(int j=1; j<=i; j++){
[Link]("*");
}
[Link]();
}
}
}
Output:
*
**
***
****
*****

Q2. Write a program to implement following pattern ***** **** *** ** *


Program:
class Pattern2 {
public static void main(String[] args) {
for(int i=5; i>=1; i--){
for(int s=5; s>i; s--){
[Link](" ");
}
for(int j=1; j<=i; j++){
[Link]("*");
}
[Link]();
}
}
}
Output:
*****
****
***
**
*

Q3. WAP using if-else to find greatest among 3 numbers


Program:
import [Link].*;
class Greatest3 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a = [Link]();
int b = [Link]();
int c = [Link]();
if(a > b && a > c)
[Link](a + " is greatest");
else if(b > c)
[Link](b + " is greatest");
else
[Link](c + " is greatest");
}
}
Output:
Input:
10 25 7
Output:
25 is greatest

Q4. WAP to find average of 5 numbers


Program:
import [Link].*;
class Average5 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int sum = 0;
for(int i=1; i<=5; i++){
sum += [Link]();
}
[Link]("Average = " + (sum/5.0));
}
}
Output:
Input:
10 20 30 40 50
Output:
Average = 30.0
Q5. WAP to find sum of first n numbers
Program:
import [Link].*;
class SumN {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
int sum = 0;
for(int i=1; i<=n; i++){
sum += i;
}
[Link]("Sum = " + sum);
}
}
Output:
Input:
5
Output:
Sum = 15

Q6. WAP by using StringBuffer & its methods


Program:
class BufferDemo {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Hello");
[Link](" World"); // Hello World
[Link](5, " Java"); // Hello Java World
[Link](); // dlroW avaJolleH
[Link](sb);
}
}
Output:
Output:
dlroW avaJolleH

Q7. WAP by using wrapper class


Program:
class WrapperDemo {
public static void main(String[] args) {
int a = 50;
Integer obj = [Link](a); // boxing
[Link](obj);
}
}
Output:
Output:
50

Q8. WAP by using while & do-while loop


Program:
class LoopDemo {
public static void main(String[] args) {
int i = 1;
while(i <= 3){
[Link]("While: " + i);
i++;
}

int j = 1;
do{
[Link]("DoWhile: " + j);
j++;
}while(j <= 3);
}
}
Output:
Output:
While: 1
While: 2
While: 3
DoWhile: 1
DoWhile: 2
DoWhile: 3
Q9. WAP to create class & object
Program:
class Student {
void show(){
[Link]("Object created");
}
public static void main(String[] args){
Student s = new Student();
[Link]();
}
}
Output:
Output:
Object created

Q10. WAP to sort (Array)


Program:
import [Link].*;
class SortArray {
public static void main(String[] args) {
int a[] = {5, 3, 1, 4, 2};
[Link](a);
for(int x : a) [Link](x+" ");
}
}
Output:
Output:
1 2 3 4 5

Q11. WAP to illustrate class within class (inner class)


Program:
class Outer {
class Inner {
void show() {
[Link]("Inner class");
}
}

public static void main(String[] args) {


Outer o = new Outer();
[Link] i = [Link] Inner();
[Link]();
}
}
Output:
Output:
Inner class

Q12. WAP to by using operator (take two numbers & find sum)
Program:
import [Link].*;
class Sum2 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a = [Link]();
int b = [Link]();
[Link](a + b);
}
}
Output:
Input:
5 7
Output:
12
Q13. WAP to take string from user & print its length
Program:
import [Link].*;
class StringLength {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String s = [Link]();
[Link]([Link]());
}
}
Output:
Input:
hello world
Output:
11

Q14. WAP to find Fibonacci series (first 10 terms)


Program:
class Fibonacci {
public static void main(String[] args) {
int a=0, b=1, c;
[Link](a+" "+b);
for(int i=3; i<=10; i++){
c = a+b;
[Link](" "+c);
a = b;
b = c;
}
}
}
Output:
Output:
0 1 1 2 3 5 8 13 21 34

Q15. WAP to find palindrome (number)


Program:
class Palindrome {
public static void main(String[] args) {
int n = 121, r, sum = 0, temp = n;
while(n > 0){
r = n % 10;
sum = sum*10 + r;
n /= 10;
}
if(sum == temp)
[Link]("Palindrome");
else
[Link]("Not Palindrome");
}
}
Output:
Output:
Palindrome
Q16. WAP by using Inheritance (simple & multilevel)
Program:
class A {
void showA(){
[Link]("Class A");
}
}

class B extends A {
void showB(){
[Link]("Class B");
}
}

class C extends B {
void showC(){
[Link]("Class C");
}

public static void main(String[] args) {


C obj = new C();
[Link]();
[Link]();
[Link]();
}
}
Output:
Output:
Class A
Class B
Class C
Q17. WAP to implement inheritance with constructor
Program:
class Parent {
Parent(){
[Link]("Parent constructor");
}
}

class Child extends Parent {


Child(){
[Link]("Child constructor");
}

public static void main(String[] args){


new Child();
}
}
Output:
Output:
Parent constructor
Child constructor

Q18. WAP for method overloading


Program:
class Overload {
void show(int a){ [Link](a); }
void show(String s){ [Link](s); }

public static void main(String[] args){


Overload o = new Overload();
[Link](10);
[Link]("Hello");
}
}
Output:
Output:
10
Hello

Q19. WAP for method overriding


Program:
class A {
void show(){
[Link]("Parent show");
}
}

class B extends A {
void show(){
[Link]("Child show");
}

public static void main(String[] args){


B b = new B();
[Link]();
}
}
Output:
Output:
Child show

Q20. WAP for packages (two-file example)


Program:
// File: mypack/[Link]
package mypack;
public class Demo {
public void show(){
[Link]("Package Demo");
}
}

// File: [Link] (in different folder)


import [Link];
class TestPackage {
public static void main(String[] args){
Demo d = new Demo();
[Link]();
}
}
Output:
Output:
Package Demo
Q21. WAP to implement interface within interface
Program:
interface A {
void show();

interface B {
void display();
}
}

class Demo implements A, A.B {


public void show(){
[Link]("Inside A");
}
public void display(){
[Link]("Inside B");
}

public static void main(String[] args){


Demo d = new Demo();
[Link]();
[Link]();
}
}
Output:
Output:
Inside A
Inside B

Q22. WAP by interface with class


Program:
interface Animal {
void sound();
}

class Dog implements Animal {


public void sound(){
[Link]("Dog Barks");
}

public static void main(String[] args){


Dog d = new Dog();
[Link]();
}
}
Output:
Output:
Dog Barks

Q23. WAP by exception handling


Program:
class ExceptionDemo {
public static void main(String[] args){
try{
int a = 10 / 0;
}
catch(Exception e){
[Link]("Error: " + e);
}
}
}
Output:
Output:
Error: [Link]: / by zero

Q24. WAP by using throws keyword


Program:
class ThrowsDemo {
static void test() throws InterruptedException {
[Link](1000);
[Link]("Executed");
}

public static void main(String[] args) throws InterruptedException {


test();
}
}
Output:
Output:
Executed

You might also like