Assesment QP - CS
Assesment QP - CS
Assesment QP - CS
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.
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:
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
Output Format
Constraints
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("======================================");
}
findThePairs(new int[] {4, -5, 9, 11, 25, 13, 12, 8}, 20);
findThePairs(new int[] {12, 23, 125, 41, -75, 38, 27, 11}, 50);
}
}
Input : 5
Output:
* * * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *
import java.io.*;
class GFG {
static void pattern(int n)
{
int i, j;
System.out.println("");
}
System.out.println("");
}
}
// main function
public static void main(String[] args)
{
pattern(7);
}
}
Mcqs
switch (f)
case 0: System.out.println("Zero");
default: System.out.println("Default");
A. Zero C. Default
2. What results from attempting to compile and run the following code?
public class Ternary
int a = 5;
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);
}
}
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[1] = 1;
a = new int[2];
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) 20 c) Compilation Error
b) 21 d) Runtime Error
8. class T {
int t = 20;
T() {
t = 40;
class Main {
T t1 = new T();
System.out.println(t1.t);
(A) 20
(B) 40
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.
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