Showing posts with label Java Interview Programs. Show all posts
Showing posts with label Java Interview Programs. Show all posts

Saturday, 7 October 2023

1. Given a list of integers, find out all the even numbers that exist in the list using Stream functions? By Java 8

 


Description:
Given a list of integers, find out all the even numbers that exist in the list using Stream functions?

Code:
?
1
2
3
4
5
import java.util.*; import java.util.stream.*; public class EvenNumber { public static void main(String args[]) { List<Integer> list = Arrays.asList(10, 15, 8, 49, 25, 98, 32); list.stream().filter(n -> n % 2 == 0).forEach(System.out::println); }

Output: 10, 8, 98, 32

Sunday, 24 March 2019

Example of Java Deserialization .


Code:
?
1
2
3
4
5
6
7

import java.io.*; class Depersist{ public static void main(String args[])throws Exception{ ObjectInputStream in=new ObjectInputStream(new FileInputStream("f.txt")); Student s=(Student)in.readObject(); System.out.println(s.id+" "+s.name); in.close(); } }
Output:: 211 govind

Example of Java Serialization.

Description:
In this example, we are going to serialize the object of Student class.
The writeObject() method of ObjectOutputStream class provides the functionality to serialize the object.
We are saving the state of the object in the file named f.txt.

Code:
?
1
2
3
4
5
6
7

import java.io.*; class Persist{ public static void main(String args[])throws Exception{ Student s1 =new Student(211,"govind"); FileOutputStream fout=new FileOutputStream("f.txt"); ObjectOutputStream out=new ObjectOutputStream(fout); out.writeObject(s1); out.flush(); System.out.println("success"); } }
Output:: success

Tuesday, 5 February 2019

12. Swapping of two Numbers in java without third variable.


Description:
Write a sample code to Swapping of two Numbers in java without third variable.


Code:
?
1
2
3
4
5
6
7
8
9
10
import java.util.Scanner; public class Swapping_Number { public static void main(String[] args) { int x,y; System.out.println("Enter x and y no: "); Scanner s=new Scanner(System.in); x=s.nextInt(); y=s.nextInt(); System.out.println("Before Swaping \n x="+x+"\n y="+y); x=x+y; y=x-y; x=x-y; System.out.println("After Swaping\n x="+x+"\n y="+y); } }


Output:
Enter x and y no: 
10
20
Before Swaping 
 x=10
 y=20
After Swaping
 x=20
 y=10

11. Palindrome program in java.


Description:
Write a sample code to Palindrome in java.


Code:
?
1
2
3
4
5
6
7
8
9
import java.util.Scanner; class Palindrome { public static void main(String args[]) { String original, reverse = ""; Scanner in = new Scanner(System.in); System.out.println("Enter a string to check :"); 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("The string is a palindrome."); else System.out.println("The string isn't a palindrome."); } }

Output:
Enter a string to check :
madam
The string is a palindrome.

Enter a string to check :
godblessgovind
The string isn't a palindrome.

10. FizzBuzz Program in Java - Interview Question


Description:
Write a sample code to FizzBuzz Program in Java.


Code:
?
1
2
3
4
5
6
7
8
9

import java.util.Scanner; public class FizzBuzz { public static void main(String[] args) { for(int i=1; i<=15; i++) { Scanner s=new Scanner(System.in); if(i%(3*5)==0)System.out.println("fizzBuzz"); else if (i% 5 ==0) System.out.println("fizz"); else if (i% 3==0)System.out.println("Buzz"); else System.out.println(i); } } }


Output:
1
2
Buzz
4
fizz
Buzz
7
8
Buzz
fizz
11
Buzz
13
14
fizzBuzz