👉 :- BCA 4 &5 semester practical exam
1. BCA practical exam- programing in java
Q.1 Write a program to find the greatest value of any three numbers.
- import java.util.Scanner;
- public class LargestNumberExample1
- {
- public static void main(String[] args)
- {
- int a, b, c, largest, temp;
- //object of the Scanner class
- Scanner sc = new Scanner(System.in);
- //reading input from the user
- System.out.println("Enter the first number:");
- a = sc.nextInt();
- System.out.println("Enter the second number:");
- b = sc.nextInt();
- System.out.println("Enter the third number:");
- c = sc.nextInt();
- //comparing a and b and storing the largest number in a temp variable
- temp=a>b?a:b;
- //comparing the temp variable with c and storing the result in the variable
- largest=c>temp?c:temp;
- //prints the largest number
- System.out.println("The largest number is: "+largest);
- }
- }
credit for answer - ANSWER WRITE BY
Q.2 Write a program to print Fabonacci series.
- class FibonacciExample1{
- public static void main(String args[])
- {
- int n1=0,n2=1,n3,i,count=10;
- System.out.print(n1+" "+n2);//printing 0 and 1
- for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed
- {
- n3=n1+n2;
- System.out.print(" "+n3);
- n1=n2;
- n2=n3;
- }
- }}
- Output :- 0 1 1 2 3 5 8 13 21 34
- credit for answer - ANSWER WRITE BY
- public class DisplayEvenNumbersExample1
- {
- public static void main(String args[])
- {
- int number= 20;
- System.out.print("List of even numbers from 1 to "+number+": ");
- for (int i=1; i<=number; i++)
- {
- //logic to check if the number is even or not
- //if i%2 is equal to zero, the number is even
- if (i%2==0)
- {
- System.out.print(i + " ");
- }
- }
- }
- }
- credit for answer - ANSWER WRITE BY
OUTPUT:- List of even numbers from 1 to 100: 2 4 6 8 10 12 14 16 18 20
Q.4 Write a program to SHOW method of overriding.
- //Java Program to demonstrate why we need method overriding
- //Here, we are calling the method of parent class with child
- //class object.
- //Creating a parent class
- class Vehicle{
- void run(){System.out.println("Vehicle is running");}
- }
- //Creating a child class
- class Bike extends Vehicle{
- public static void main(String args[]){
- //creating an instance of child class
- Bike obj = new Bike();
- //calling the method with child class instance
- obj.run();
- }
- }
ans:- .
Single Inheritance Example
When a class inherits another class, it is known as a single inheritance. In the example given below, Dog class inherits the Animal class, so there is the single inheritance.
- class Animal{
- void eat(){System.out.println("eating...");}
- }
- class Dog extends Animal{
- void bark(){System.out.println("barking...");}
- }
- class TestInheritance{
- public static void main(String args[]){
- Dog d=new Dog();
- d.bark();
- d.eat();
- }}
output- barking... eating...credit - ANSWER WRITE BY
Q.6 Write a program to create thread.
Java Threads | How to create a thread in Java
There are two ways to create a thread:
- By extending Thread class
- By implementing Runnable interface.
There are two ways to create a thread:
- By extending Thread class
- By implementing Runnable interface.
Thread class:
Thread class provide constructors and methods to create and perform operations on a thread.Thread class extends Object class and implements Runnable interface.
Thread class provide constructors and methods to create and perform operations on a thread.Thread class extends Object class and implements Runnable interface.
Commonly used Constructors of Thread class:
- Thread()
- Thread(String name)
- Thread(Runnable r)
- Thread(Runnable r,String name)
1) Java Thread Example by extending Thread class
- class Multi extends Thread{
- public void run(){
- System.out.println("thread is running...");
- }
- public static void main(String args[]){
- Multi t1=new Multi();
- t1.start();
- }
- }
credit - ANSWER WRITE
Q.7 write a program to show exception handling using try catch states.Exception Handling in Java
The Exception Handling in Java is one of the powerful mechanism to handle the runtime errors so that the normal flow of the application can be maintained.
In this tutorial, we will learn about Java exceptions, it's types, and the difference between checked and unchecked exceptions.
- public class JavaExceptionExample{
- public static void main(String args[]){
- try{
- //code that may raise exception
- int data=100/0;
- }catch(ArithmeticException e){System.out.println(e);}
- //rest code of the program
- System.out.println("rest of the code...");
- }
- }
OUTPUT ;- Exception in thread main java.lang.ArithmeticException:/ by zerorest of the code...Q.8 write a program FOR implement interface.How to declare an interface?
An interface is declared by using the interface keyword. It provides total abstraction; means all the methods in an interface are declared with the empty body, and all the fields are public, static and final by default. A class that implements an interface must implement all the methods declared in the interface.
Syntax:
- interface printable{
- void print();
- }
- class A6 implements printable{
- public void print(){System.out.println("Hello");}
- public static void main(String args[]){
- A6 obj = new A6();
- obj.print();
- }
- }
output:- HelloQ.9 Write a program to SHOW Method of overloading.Method of overloading in Java
If a class has multiple methods having same name but different in parameters, it is known as Method Overloading.
- class Adder{
- static int add(int a,int b){return a+b;}
- static int add(int a,int b,int c){return a+b+c;}
- }
- class TestOverloading1{
- public static void main(String[] args){
- System.out.println(Adder.add(11,11));
- System.out.println(Adder.add(11,11,11));
- }}
Output:-2233Q.10 Write a program to define Package.1) Using packagename.*
If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages.
The import keyword is used to make the classes and interface of another package accessible to the current package.
Example of package that import the packagename.*//save by B.java
- //save by A.java
- package pack;
- public class A{
- public void msg(){System.out.println("Hello");}
- }
- package mypack;
- import pack.*;
- class B{
- public static void main(String args[]){
- A obj = new A();
- obj.msg(); }}
OUTPUT:- Hello
#javaprograming #amantechgwalior


Comments
Post a Comment