0% found this document useful (0 votes)
3 views2 pages

Java Coding Questions Prep

The document contains Java code snippets demonstrating various programming concepts such as reversing a number, creating classes, method overloading and overriding, checking for prime numbers, and swapping two numbers without a third variable. Each section provides a brief example of the respective concept with accompanying code. The examples illustrate fundamental programming techniques in Java.
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)
3 views2 pages

Java Coding Questions Prep

The document contains Java code snippets demonstrating various programming concepts such as reversing a number, creating classes, method overloading and overriding, checking for prime numbers, and swapping two numbers without a third variable. Each section provides a brief example of the respective concept with accompanying code. The examples illustrate fundamental programming techniques in Java.
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/ 2

how to reverse a string:

import java.util.*;
class main{
public static void main(String[]args){
int reverse=0;
Scanner sc=new Scanner(System.in);
System.out.println("please give the number:");
int i=sc.nextInt();
while(i!=0){
reverse=reverse*10+i%10;
i=i/10;
}
System.out.println("Number after reverse:"+reverse);
}

How to create an class :

class main{
int x=5;
}

How to create an class in Object:

class main{
int x=5;
public static void main(String[]args){
main myobj=new main();
System.out.println(myobj.x);
}
}

Method overloading:

public class Calculator{


public int add(int num1,int num2){
return num1+num2;
}
public double add(double num1,double num2){
return num1+num2;
}
}

Method overriding:

class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {


@Override
void sound() {
System.out.println("Dog barks");
}
}

public class Main {


public static void main(String[] args) {
Animal animal = new Animal();
animal.sound();

Animal dog = new Dog();


dog.sound();
}
}

Print prime or not:

import java.util.Scanner;

public class PrimeCheck {


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

boolean isPrime = num > 1;


for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}

System.out.println(isPrime ? num + " is prime." : num + " is not prime.");


}
}

Swap two numbers with out using Third variable:

public class SwapNumbers{


public static void main(String[]args){
int a=10;
int b=20;
System.out.println("a is"+a+"b is"+b);
a=a+b;
b=a-b;
a=a-b;
System.out.println("After Swapping,a is"+a+" and b is"+b);
}
}

You might also like