0% found this document useful (0 votes)
26 views10 pages

Internal Assessment Oops

Uploaded by

pranay sharma
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)
26 views10 pages

Internal Assessment Oops

Uploaded by

pranay sharma
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/ 10

Prashant Khanal

2204920100102
Section : A1 (40)

OOPS QUIZ (Internal Assessment)


1. Sort given matrix
import java.util.Arrays;

class Pranay {

static void sortMat(int mat[][], int n) {

int[] temp = new int[n * n];

int k = 0;

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

temp[k++] = mat[i][j];

Arrays.sort(temp);

k = 0;

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

mat[i][j] = temp[k++];

public static void main(String[] args) {

int n = 3;

int[][] matrix = {

{5, 4, 7},

{1, 3, 8},

{2, 9, 6}

};

Input : mat[][] = { {5, 4, 7}, {1, 3, 8}, {2, 9, 6} }

Output: 1 2 3 4 5 6 7 8 9
2. Find Second largest element from array ?
public class Pranay {

static int findSecondLargest(int[] arr) {

int firstLargest = Integer.MIN_VALUE;

int secondLargest = Integer.MIN_VALUE;

for (int i = 0; i < arr.length; i++) {

if (arr[i] > firstLargest) {

secondLargest = firstLargest;

firstLargest = arr[i];

} else if (arr[i] > secondLargest && arr[i] != firstLargest) {

secondLargest = arr[i];

return secondLargest;

public static void main(String[] args) {

int[] arr = {12, 35, 1, 10, 34, 1};

int secondLargest = findSecondLargest(arr);

System.out.println("Second Largest element is: " + secondLargest);

Input: arr[] = {12, 35, 1, 10, 34, 1}

Output : Second largest element is 34


3. Move all zeroes to end of array.
public class Pranay {

static void moveZeroesToEnd(int[] arr) {

int n = arr.length;

int count = 0;

for (int i = 0; i < n; i++) {

if (arr[i] != 0) {

arr[count++] = arr[i];

while (count < n) {

arr[count++] = 0;

public static void main(String[] args) {

int[] arr = {1, 2, 0, 4, 3, 0, 5, 0};

moveZeroesToEnd(arr);

System.out.println("Array after moving zeroes to the end:");

for (int i : arr) {

System.out.print(i + " ");

Input : arr[] = {1, 2, 0, 4, 3, 0, 5, 0};

Output : arr[] = {1, 2, 4, 3, 5, 0, 0, 0};


4. Find out duplicate fruits.
import java.util.HashMap;

import java.util.Map;

public class Pranay {

static void findDuplicateFruits(String[] fruits) {

Map<String, Integer> fruitCountMap = new HashMap<>();

for (String fruit : fruits) {

fruitCountMap.put(fruit, fruitCountMap.getOrDefault(fruit, 0) + 1);

System.out.println("Duplicates:");

for (Map.Entry<String, Integer> entry : fruitCountMap.entrySet()) {

if (entry.getValue() > 1) {

System.out.println(entry.getKey() + ":" + entry.getValue());

public static void main(String[] args) {

String[] fruits = {"apple", "orange", "banana", "apple", "mango", "orange", "kiwi"};

findDuplicateFruits(fruits);

Input : "apple", "orange", "banana", "apple", "mango", "orange", "kiwi"

Output : Duplicates: [apple:2, orange:2]


5. Write a program to check if two given strings are anagrams of each other.
import java.util.Arrays;

public class Pranay {

static boolean areAnagrams(String str1, String str2) {

str1 = str1.replaceAll("\\s", "").toLowerCase();

str2 = str2.replaceAll("\\s", "").toLowerCase();

if (str1.length() != str2.length()) {

return false;

char[] arr1 = str1.toCharArray();

char[] arr2 = str2.toCharArray();

Arrays.sort(arr1);

Arrays.sort(arr2);

return Arrays.equals(arr1, arr2);

public static void main(String[] args) {

String str1 = "eat";

String str2 = "tea";

if (areAnagrams(str1, str2)) {

System.out.println(str1 + " and " + str2 + " are anagrams.");

} else {

System.out.println(str1 + " and " + str2 + " are not anagrams.");

String str3 = "Silent";

String str4 = "listen";

Input : str1 = "eat", str2 = "tea"

Output : eat and tea are anagrams.


6. Write a Java program to check if a given string of parentheses is balanced.
import java.util.Stack;

public class Pranay {

static boolean isBalanced(String str) {

Stack<Character> stack = new Stack<>();

for (char c : str.toCharArray()) {

if (c == '(' || c == '{' || c == '[') {

stack.push(c);

} else if (c == ')' || c == '}' || c == ']') {

if (stack.isEmpty()) {

return false;

char top = stack.pop();

if ((c == ')' && top != '(') ||

(c == '}' && top != '{') ||

(c == ']' && top != '[')) {

return false;

return stack.isEmpty();

public static void main(String[] args) {

String str1 = "([])";

if (isBalanced(str1)) {

System.out.println(str1 + " is balanced.");

} else {

Input : str = "([])"

Output : "([])" is balanced.


7.Rotate an array to the right by ‘k’ steps,where ‘k’ is a non-negative number.
import java.util.Arrays;

public class Pranay {

static void rotateArray(int[] nums, int k) {

int n = nums.length;

k = k % n; // If k is larger than array length, reduce it

reverse(nums, 0, n - 1);

reverse(nums, 0, k - 1);

reverse(nums, k, n - 1);

static void reverse(int[] nums, int start, int end) {

while (start < end) {

int temp = nums[start];

nums[start] = nums[end];

nums[end] = temp;

start++;

end--;

public static void main(String[] args) {

int[] array = {1, 2, 3, 4, 5, 6};

int k = 4;

System.out.println("Original Array: " + Arrays.toString(array));

rotateArray(array, k);

System.out.println("Rotated Array by " + k + " steps: " + Arrays.toString(array));

Input : array = [1, 2, 3, 4, 5, 6] k=4

Output :

Original Array: [7, 8, 9, 10, 11]

Rotated Array by 2 steps: [10, 11, 7, 8, 9]


8.Create a Java Singleton class
public class Singleton {

private static Singleton instance;

private Singleton() {

public static Singleton getInstance() {

if (instance == null) {

instance = new Singleton();

return instance;

public void exampleMethod() {

System.out.println("This is an example method of the Singleton class.");

public static void main(String[] args) {

Singleton singleton = Singleton.getInstance();

singleton.exampleMethod();

}
9.From a given array of natural numbers, return the distance between the
two closest numbers.
import java.util.Arrays;

public class ClosestNumbers {

static int distClosestNumbers(int[] nums) {

if (nums == null || nums.length < 2) {

return -1;

Arrays.sort(nums);

int minDistance = Integer.MAX_VALUE;

for (int i = 1; i < nums.length; i++) {

int distance = nums[i] - nums[i - 1];

if (distance < minDistance) {

minDistance = distance;

return minDistance;

public static void main(String[] args) {

int[] testArray = {3, 9, 50, 15, 99, 7, 98, 65};

int result = distClosestNumbers(testArray);

System.out.println("Minimum distance between closest numbers: " + result);

Input : int[] testArray = {3, 9, 50, 15, 99, 7, 98, 65};

Output : int[] testArray = {3, 9, 50, 15, 99, 7, 98, 65};


10. How do you remove all occurrences of a given character from the input
string?
public class RemoveCharacter {

public static void main(String[] args) {

String str1 = "Australia";

char charToRemove = 'a';

str1 = str1.replace(String.valueOf(charToRemove), "");

System.out.println(str1);

Input :

String str1 = "Australia";

char charToRemove = 'a';

Output : ustrli

You might also like