Sie sind auf Seite 1von 19

1. String in Java is a?

a) class
b) object
c) variable
d) character array
2. Which of these method of String class is used to obtain character at specified index?
a) char()
b) Charat()
c) charat()
d) charAt()
3. Which of these keywords is used to refer to member of base class from a sub class?
a) upper
b) super
c) this
d) none of the mentioned
4. Which of these method of String class can be used to test to strings for equality?
a) isequal()
b) isequals()
c) equal()
d) equals()
5. What is the output of this program?
class string_demo
{
public static void main(String args[])
{
String obj = "I" + "like" + "Java";
System.out.println(obj);
}
}

6. What is the output of this program?


class string_class
{
public static void main(String args[])
{
String obj = "I LIKE JAVA";
System.out.println(obj.charAt(3));
}
}
7.Which of these keyword must be used to inherit a class?
a) super
b) this
c) extent
d) extends
8.Which of these keywords is used to refer to member of base class from a sub class?
a) upper
b) super
c) this
d) none of the mentioned

9. Which of these is correct way of inheriting class A by class B?


a) class B + class A {}
b) class B inherits class A {}
c) class B extends A {}
d) class B extends class A {}

10. What is the output of this program?


class A
{
int i;
void display()
{
System.out.println(i);
}
}
class B extends A
{
int j;
void display()
{
System.out.println(j);
}
}
class inheritance_demo
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
11. What is the output of this program?
class A
{
public int i;
protected int j;
}
class B extends A
{
int j;
void display()
{
super.j = 3;
System.out.println(i + " " + j);
}
}
class Output
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
12.What is the process of defining a method in subclass having same name & type signature
as a method in its superclass?
a) Method overloading
b) Method overriding
c) Method hiding

.13. Which of these keywords can be used to prevent Method overriding?


a) static
b) constant
c) protected
d) final

14.What is the output of this program?


class Alligator
{
public static void main(String[] args)
{
int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}};
int [][]y = x;
System.out.println(y[2][1]);
}
}

15. What is the output of this program?


class A
{
int i;
public void display()
{
System.out.println(i);
}
}
class B extends A
{
int j;
public void display()
{
System.out.println(j);
}
}
class Dynamic_dispatch
{
public static void main(String args[])
{
B obj2 = new B();
obj2.i = 1;
obj2.j = 2;
A r;
r = obj2;
r.display();
}
}

class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello World");
}
}
---------------------------------------------------------
/ If else in Java code
import java.util.Scanner;

class IfElse {
public static void main(String[] args) {
int marksObtained, passingMarks;

passingMarks = 40;

Scanner input = new Scanner(System.in);

System.out.println("Input marks scored by you");

marksObtained = input.nextInt();

if (marksObtained >= passingMarks) {


System.out.println("You passed the exam.");
}
else {
System.out.println("Unfortunately, you failed to pass the exam.");
}
}
}
--------------------------------------------------------

//Java for loop program


class ForLoop {
public static void main(String[] args) {
int c;

for (c = 1; c <= 10; c++) {


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

import java.util.Scanner;

class WhileLoop {
public static void main(String[] args) {
int n;

Scanner input = new Scanner(System.in);


System.out.println("Input an integer");

while ((n = input.nextInt()) != 0) {


System.out.println("You entered " + n);
System.out.println("Input an integer");
}

System.out.println("Out of loop");
}
}
class Alphabets
{
public static void main(String args[])
{
char ch;

for( ch = 'a' ; ch <= 'z' ; ch++ )


System.out.println(ch);
}
}

Printing alphabets using a while loop (Only the body of the


main method is shown):

char c = 'a';

while (c <= 'z') {


System.out.println(c);
c++;
}

Using a do while loop:

char c = 'A';

do {
System.out.println(c);
c++;
} while (c <= 'Z');
import java.util.Scanner;

class GetInputFromUser
{
public static void main(String args[])
{
int a;
float b;
String s;

Scanner in = new Scanner(System.in);

System.out.println("Enter an integer");
a = in.nextInt();
System.out.println("You entered integer "+a);

System.out.println("Enter a float");
b = in.nextFloat();
System.out.println("You entered float "+b);

System.out.println("Enter a string");
s = in.nextLine();
System.out.println("You entered string "+s);
}
}

import java.util.Scanner;
class MultiplicationTable
{
public static void main(String args[])
{
int n, c;
System.out.println("Enter an integer to print it's multiplication table");
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println("Multiplication table of "+n+" is :-");

for ( c = 1 ; c <= 10 ; c++ )


System.out.println(n+"*"+c+" = "+(n*c));
}
}

import java.util.Scanner;

class OddOrEven
{
public static void main(String args[])
{
int x;
System.out.println("Enter an integer to check if it is odd or even ");
Scanner in = new Scanner(System.in);
x = in.nextInt();

if ( x % 2 == 0 )
System.out.println("You entered an even number.");
else
System.out.println("You entered an odd number.");
}
}

Swapping using temporary or third variable

import java.util.Scanner;

class SwapNumbers
{
public static void main(String args[])
{
int x, y, temp;
System.out.println("Enter x and y");
Scanner in = new Scanner(System.in);

x = in.nextInt();
y = in.nextInt();

System.out.println("Before Swapping\nx = "+x+"\ny = "+y);

temp = x;
x = y;
y = temp;

System.out.println("After Swapping\nx = "+x+"\ny = "+y);


}
}

Swapping without temporary variable

import java.util.Scanner;

class SwapNumbers
{
public static void main(String args[])
{
int x, y;
System.out.println("Enter x and y");
Scanner in = new Scanner(System.in);

x = in.nextInt();
y = in.nextInt();

System.out.println("Before Swapping\nx = "+x+"\ny = "+y);

x = x + y;
y = x - y;
x = x - y;

System.out.println("After Swapping\nx = "+x+"\ny = "+y);


}
}
import java.util.Scanner;

class Factorial
{
public static void main(String args[])
{
int n, c, fact = 1;

System.out.println("Enter an integer to calculate it's factorial");


Scanner in = new Scanner(System.in);

n = in.nextInt();

if (n < 0)
System.out.println("Number should be non-negative.");
else
{
for (c = 1; c <= n; c++)
fact = fact*c;

System.out.println("Factorial of "+n+" is = "+fact);


}
}
}
import java.util.Scanner;

class ArmstrongNumber
{
public static void main(String args[])
{
int n, sum = 0, temp, remainder, digits = 0;

Scanner in = new Scanner(System.in);


System.out.println("Input a number to check if it is an Armstrong number");
n = in.nextInt();

temp = n;

// Count number of digits

while (temp != 0) {
digits++;
temp = temp/10;
}

temp = n;

while (temp != 0) {
remainder = temp%10;
sum = sum + power(remainder, digits);
temp = temp/10;
}

if (n == sum)
System.out.println(n + " is an Armstrong number.");
else
System.out.println(n + " isn't an Armstrong number.");
}

static int power(int n, int r) {


int c, p = 1;

for (c = 1; c <= r; c++)


p = p*n;

return p;
}
}

import java.util.*;

class ReverseString
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);

System.out.println("Enter a string to reverse");


original = in.nextLine();

int length = original.length();

for ( int i = length - 1 ; i >= 0 ; i-- )


reverse = reverse + original.charAt(i);

System.out.println("Reverse of entered string is: "+reverse);


}
}

import java.util.*;

class Palindrome
{
public static void main(String args[])
{
String original, reverse = ""; // Objects of String class
Scanner in = new Scanner(System.in);

System.out.println("Enter a string to check if it is a palindrome");


original = in.nextLine();

int length = original.length();


for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + original.charAt(i);

if (original.equals(reverse))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string isn't a palindrome.");

}
}

import java.util.Scanner;

class ReverseNumber
{
public static void main(String args[])
{
int n, reverse = 0;

System.out.println("Enter a number to reverse");


Scanner in = new Scanner(System.in);
n = in.nextInt();

while(n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}

System.out.println("Reverse of the number is " + reverse);


}
}

To find Strong Number


import java.util.scanner;

public class strongnumber

public static void

main(string args[])

strongnumber ss=new

strongnumber();

int a,b,r,s=0;

scanner sl=new scanner(system.in);

system.out.println(“Enter a number”);

b=sl.nextInt();

a=b;

while(b>0)

{
r=b%10;

s=s+ss.fact(r);

b=b/10;

if(a==s)

system.out.println(a+” is a stong number);

else

system.out.println(a+”is not a strong number);

int fact(int i)

int f,j;

f=1;

for(j=i;j>0;j--)

f=f*j;

return f;

GCD of Two Numbers:

import java.util.P;

public class gcdoftwonumbers

public static void

main(string args[])

{
int a,b,gcd;

a=b=gcd=0;

scanner s=new scanner(system.in);

system.out.println(“enter two num);

a=s.nextInt();

b=s.next.Int();

while(a!=0)

gcd=a;

a=b%a;

b=gcd;

system.out.println(“GCD”+gcd);

Das könnte Ihnen auch gefallen