0% found this document useful (0 votes)
3 views

Java Slips.docx

Uploaded by

VISHAL SHINDE
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)
3 views

Java Slips.docx

Uploaded by

VISHAL SHINDE
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
You are on page 1/ 20

Slip1 Q.1.

Core Java: A) Write a ‘java’ program to


display characters from ‘A’ to ‘Z’
import java.io.*;
class slip1
{
public static void main(String[] args) {
char ch;
for(ch = 'A'; ch <= 'Z';ch++)
System.out.println("the alphabets are :"+ch);
}
}

Output:
C:\Program Files\Java\jdk1.8.0_144\bin>javac
slip1.java

C:\Program Files\Java\jdk1.8.0_144\bin>java slip1


the alphabets are :A
the alphabets are :B
the alphabets are :C
the alphabets are :D
the alphabets are :E
the alphabets are :F
the alphabets are :G
the alphabets are :H
the alphabets are :I
the alphabets are :J
the alphabets are :K
the alphabets are :L
the alphabets are :M
the alphabets are :N
the alphabets are :O
the alphabets are :P
the alphabets are :Q
the alphabets are :R
the alphabets are :S
the alphabets are :T
the alphabets are :U
the alphabets are :V
the alphabets are :W
the alphabets are :X
the alphabets are :Y
the alphabets are :Z

Slip3 Q.1. Core Java: A) Write a ‘java’ program to


check whether given number is Armstrong or not.
(Use static keyword)

import java.io.*;
import java.util.*;
class slip3
{
public static void main(String arg[])
{
int check,rem=0;
int sum=0,num;
System.out.println("enter the number to check
armstrong or not");
Scanner sc=new Scanner(System.in);
num=sc.nextInt();
check=num;
while(check!=0)
{
rem=check%10;
sum=sum+(rem*rem*rem);
check=check/10;
}
if(num==sum)
System.out.println("the number is armstrong");
else
System.out.println("the number is not armstrong");
}
}

Output
C:\Program Files\Java\jdk1.8.0_144\bin>javac
slip3.java

C:\Program Files\Java\jdk1.8.0_144\bin>java slip3


enter the number to check armstrong or not
153
the number is Armstrong
C:\Program Files\Java\jdk1.8.0_144\bin>java slip3
enter the number to check armstrong or not
432
the number is not armstrong

Slip4 Q.1. Core Java: A) Write a java program to


display alternate character from a given string.[15M]

import java.io.*;
public class slip4
{
public static void main( String arg[])
{
String s="hello world!";
for(int i=0;i<s.length();i+=2)
{
System.out.println(s.charAt(i));
}
}
}

Output:
C:\Users\BBA11>cd C:\Program
Files\Java\jdk1.8.0_144\bin

C:\Program Files\Java\jdk1.8.0_144\bin>javac
slip4.java

C:\Program Files\Java\jdk1.8.0_144\bin>java slip4


h
l
o
w
r
d

slip8 Q.1. Core Java: A) Define an Interface Shape with


abstract method area(). Write a java program to
calculate an area of Circle and Sphere.(use final
keyword) [15 M]
import java.io.*;
import java.util.*;
interface shape
{
float pi=3.14f;
void area();
}
class circle implements shape
{
int r;
circle(int r)
{
this.r=r;
}
public void area()
{
System.out.println("Area of circle is :"+(pi*r*r));
}
}
class sphere implements shape
{
int r;
sphere(int r)
{
this.r=r;
}
public void area()
{
System.out.println("Area of sphere is :"+(4*pi*r*r));
}
}
class slip8
{
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the redius for circle :");
int n=sc.nextInt();
shape s;
s=new circle(n);
s.area();
System.out.println("enter the redius for sphere :");
n=sc.nextInt();
s=new sphere(n);
s.area();
}
}

Output:
C:\Program Files\Java\jdk1.8.0_144\bin>java slip8
enter the redius for circle :
3
Area of circle is :28.26
enter the redius for sphere :
6
Area of sphere is :452.16

Slip13 Q.1. Core Java: A) Write a java program to


accept ‘n’ integers from the user & store them in an
ArrayList collection. Display the elements of ArrayList
collection in reverse order. [15 M]

import java.io.*;
import java.util.*;
class slip13
{
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the limit of array list");
int n=sc.nextInt();
ArrayList al=new ArrayList();
System.out.println("enter the elements of arry list");
for(int i=0;i<n;i++)
{
String el=sc.next();
al.add(el);
}
System.out.println("the original array list is :"+al);
Collections.reverse(al);
System.out.println("the original reverse array list is
:"+al);
}
}

Output:
C:\Program Files\Java\jdk1.8.0_144\bin>java slip13
enter the limit of array list
4
enter the elements of arry list
abcd
the original array list is :[a, b, c, d]
the original reverse array list is :[d, c, b, a]

Slip19 Q.1. Core Java: A) Write a Java program to


display Fibonacci series using function. [15 M]

import java.io.*;
class slip19
{
public static void main(String ar[])
{
int n=10 , ft=0, st=1;
System.out.println("fibonacci series are" +n+
"terms:");
for(int i=1;i<=n;++i)
{
System.out.print(ft+",");
int nt=ft+st;
ft=st;
st=nt;
}
}
}

output:
C:\Users\BBA11>cd C:\Program
Files\Java\jdk1.8.0_144\bin

C:\Program Files\Java\jdk1.8.0_144\bin>javac
slip19.java
C:\Program Files\Java\jdk1.8.0_144\bin>java slip19
fibonacci series are10terms:
0,1,1,2,3,5,8,13,21,34,
C:\Program Files\Java\jdk1.8.0_144\bin>

Slip21 Q.1. Core Java: A) Write a java program to


display each word from a file in reverse order. [15 M]

import java.io.*;
class slip21
{
public static void main(String arg[])throws
IOException
{
FileReader fr=new FileReader("input.txt");
FileWriter fw=new FileWriter("output.txt");
BufferedReader b=new BufferedReader(fr);
String data;
String reverse;
while((data=b.readLine())!=null)
{
String words[]=data.split(" ");
for(String a:words)
{
StringBuilder builder=new StringBuilder(a);
System.out.println(builder.reverse().toString());
}
}
}
}

input.txt save in bin directory


hello world

output.txt save in bin directory

output:
C:\Users\BBA11>cd C:\Program
Files\Java\jdk1.8.0_144\bin

C:\Program Files\Java\jdk1.8.0_144\bin>javac
slip21.java

C:\Program Files\Java\jdk1.8.0_144\bin>java slip21


olleh
dlrow

slip24 Q.1. Core Java: A) Write a java program to


count number of digits, spaces and characters from a
file. [15 M]

import java.io.*;
class slip24
{
public static void main(String arg[])throws Exception
{
int cnt=0,lcnt=1,wnt=0,c;
FileInputStream fin=new FileInputStream("abc.txt");
while((c=fin.read())!=-1)
{
cnt++;
if(c==32||c==13)
wnt++;
if(c==1)
lcnt++;
}
System.out.println("number of character are"+cnt);
System.out.println("number of words are"+lcnt);
System.out.println("number of lines are"+wnt);
}
}

abc.txt
Facts about the History of India
India is the largest and the oldest civilisation in the
world.
India holds the record of not invading any other
country in the last 10,000 years of her history.
Interestingly, India was one of the richest countries on
earth before the British invaded it in the early 17th
century, and was also one of the first countries in the
..

Output:
C:\Program Files\Java\jdk1.8.0_144\bin>javac
slip24.java

C:\Program Files\Java\jdk1.8.0_144\bin>java slip24


number of character are368
number of words are1
number of lines are66

Slip25 Q.1. Core Java: A) Write a java program to check


whether given string is palindrome or not. [15 M]

import java.io.*;
class slip25
{
public static void main(String ar[])throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("enter the string ");

String str=br.readLine();
String temp=str;
StringBuffer sb=new StringBuffer(str);
sb.reverse();
str=sb.toString();
if(temp.equalsIgnoreCase(str))
System.out.println("the string is palindrome");
else
System.out.println("the string is not palindrome");
}
}
output:
C:\Program Files\Java\jdk1.8.0_144\bin>javac
slip25.java

C:\Program Files\Java\jdk1.8.0_144\bin>java slip25


enter the string
madam
the string is palindrome

Slip26 Q.1. Core Java: A) Write a java program to display


ASCII values of the characters from a file. [15 M]

import java.io.*;
class slip26
{
public static void main(String arg[])
{
char ch1='a';
char ch2='b';
char ch3='B';
int asciivalue1=ch1;
int asciivalue2=ch2;
int asciivalue3=ch3;
System.out.println("the ascii value of " +ch1+ "value:
"+asciivalue1);
System.out.println("the ascii value of " +ch2+ "value:
"+asciivalue2);
System.out.println("the ascii value of " +ch3+ "value:
"+asciivalue3);
}
}

output:
C:\Program Files\Java\jdk1.8.0_144\bin>javac
slip26.java

C:\Program Files\Java\jdk1.8.0_144\bin>java slip26


the ascii value of avalue: 97
the ascii value of bvalue: 98
the ascii value of Bvalue: 66

You might also like