Assesment QP - CS

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

1.

Remove common characters and concatenate

Two strings are given. Modify 1st string such that all the common characters of the
2nd strings have to be removed and the uncommon characters of the 2nd string have
to be concatenated with uncommon characters of the 1st string.

Note: If the modified string is empty then print '-1'.

Input:

The first line consists of an integer T i.e number of test cases. The first line of each
test case consists of a string s1.The next line consists of a string s2.

Output:

Print the required output.

Constraints:

1<=T<=200

1<=|Length of Strings|<=100

Example:

Input:

aacdb

gafd

abcs

cxzca

Output:

cbgf

bsxz
2. Write a program to find the GCD of the given two numbers.

Input Format

Single line input separated by space contains two integer values X, Y.

Output Format

Single integer value indicates the GCD of given two Values.

Constraints

Should not contain any negative input.

class Test
{
static int gcd(int a, int b)
{
// Everything divides 0
if (a == 0 || b == 0)
return 0;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
// Driver method
public static void main(String[] args)
{
int a = 98, b = 56;
System.out.println("GCD of " + a +" and " + b + " is " + gcd(a, b));
}
}
3. Write a java program to find all pairs of elements in an integer array whose sum is
equal to a given number?

Output :
Input Array : [4, 6, 5, -10, 8, 5, 20]
Input Number : 10
Pairs of elements whose sum is 10 are :
4 + 6 = 10
5 + 5 = 10
-10 + 20 = 10
======================================
Input Array : [4, -5, 9, 11, 25, 13, 12, 8]
Input Number : 20
Pairs of elements whose sum is 20 are :
-5 + 25 = 20
9 + 11 = 20
12 + 8 = 20
======================================
Input Array : [12, 13, 40, 15, 8, 10, -15]
Input Number : 25
Pairs of elements whose sum is 25 are :
12 + 13 = 25
40 + -15 = 25
15 + 10 = 25
======================================
Input Array : [12, 23, 125, 41, -75, 38, 27, 11]
Input Number : 50
Pairs of elements whose sum is 50 are :
12 + 38 = 50
23 + 27 = 50
125 + -75 = 50
SOLUTION

import java.util.Arrays;
public class MainClass
{
static void findThePairs(int inputArray[], int inputNumber)
{
System.out.println("Input Array : "+Arrays.toString(inputArray));

System.out.println("Input Number : "+inputNumber);

System.out.println("Pairs of elements whose sum is "+inputNumber+" are : ");

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


{
for (int j = i+1; j < inputArray.length; j++)
{
if(inputArray[i]+inputArray[j] == inputNumber)
{
System.out.println(inputArray[i]+" + "+inputArray[j]+" =
"+inputNumber);
}
}
}

System.out.println("======================================");
}

public static void main(String[] args)


{
findThePairs(new int[] {4, 6, 5, -10, 8, 5, 20}, 10);

findThePairs(new int[] {4, -5, 9, 11, 25, 13, 12, 8}, 20);

findThePairs(new int[] {12, 13, 40, 15, 8, 10, -15}, 25);

findThePairs(new int[] {12, 23, 125, 41, -75, 38, 27, 11}, 50);
}
}

4. Program to print following pattern:


Examples :

Input : 5

Output:
* * * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *

import java.io.*;
class GFG {
static void pattern(int n)
{
int i, j;

// This is upper half of pattern


for (i = 1; i <= n; i++) {
for (j = 1; j <= (2 * n); j++) {

// Left part of pattern


if (i > (n - j + 1))
System.out.print(" ");
else
System.out.print("*");

// Right part of pattern


if ((i + n) > j)
System.out.print(" ");
else
System.out.print("*");
}

System.out.println("");
}

// This is lower half of pattern


for (i = 1; i <= n; i++) {
for (j = 1; j <= (2 * n); j++) {

// Right Part of pattern


if (i < j)
System.out.print(" ");
else
System.out.print("*");

// Left Part of pattern


if (i <= ((2 * n) - j))
System.out.print(" ");
else
System.out.print("*");
}

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

// main function
public static void main(String[] args)
{
pattern(7);
}
}

Mcqs

1. float f = new float("12");

switch (f)

case 12: System.out.println("Twelve");

case 0: System.out.println("Zero");

default: System.out.println("Default");

A. Zero C. Default

B. Twelve D. Compilation fails

2. What results from attempting to compile and run the following code?
public class Ternary

public static void main(String args[])

int a = 5;

System.out.println("Value is - " + ((a < 5) ? 9.9 : 9));

A. prints: Value is - 9 C. Compilation error

B. prints: Value is - 5 D. None of these

3. What is the output of the following program?

class A{
public static void main(String args[]){
byte b;
int i = 258;
double d = 325.59;
b = (byte) i;
System.out.print(b);
i = (int) d;
System.out.print(i);
b = (byte) d;
System.out.print(b);
}
}

A. 258 325 325 C. 2 325 69

B. 258 326 326 D. Error


4. What is the output of the following Java program?

1. class Test
2. {
3. public static void main (String args[])
4. {
5. for(int i=0; 0; i++)
6. {
7. System.out.println("Hello VITians");
8. }
9. }
10. }

A. prints: Hello VITians once C. Compilation error

B. prints: Hello VITians infinite times D. None of these


till stack is full

5. What is the output of the following code?

public class Test{

public static void main(String[] args){

int[] a = new int[4];

a[1] = 1;

a = new int[2];

System.out.println("a[1] is " + a[1]);

a) The program has a compile error c) a[1] is 0

b) The program has a runtime error d) a[1] is 1


6. What is the output of the following code?

class Base
{
public void baseMethod()
{
System.out.println("Base Method called ...");
}
}
class Derived extends Base
{
public void baseMethod()
{
System.out.println("Derived method called ...");
}
}
public class Test
{
public static void main (String args[])
{
Base b = new Derived();
b.baseMethod();
}
}

a) Base Method called ... c) Derived method called ...


Derived method called ... Base Method called ...
b) Derived method called ... d) Compilation Error

7. What is the output of the following code?

public class HelloWorld{


public static void main(String []args){
final int i;
i = 20;
i++;
System.out.println(i);
}
}

a) 20 c) Compilation Error
b) 21 d) Runtime Error

8. class T {

int t = 20;

T() {

t = 40;

class Main {

public static void main(String args[]) {

T t1 = new T();

System.out.println(t1.t);

(A) 20

(B) 40

(C) Compiler Error

9. What is the result?

public class Test {


public static void main(String [] args) {
int x = 5;
boolean b1 = true;
boolean b2 = false;

if ((x == 4) && !b2 )


System.out.print("1 ");
System.out.print("2 ");
if ((b2 = true) && b1 )
System.out.print("3 ");
}
}

CHOICE :

A. 2
B. 3
C. 1 2
D. 2 3
E. 1 2 3
F. Compilation fails.
G. An exception is thrown at runtime.

10. What is the result?


class Hexy {
public static void main(String[] args) {
Integer i = 42;
String s = (i<40)?"life":(i>50)?"universe":"everything";
System.out.println(s);
}}

A. null
B. life
C. universe
D. everything
E. Compilation fails.
F. An exception is thrown at runtime.

Answers

1. d
2. d
3. c
4. C
5. C
6. B
7. D
8. A
9. D
10.D

You might also like