Technical 2
Technical 2
TCS
DIGITAL
TRAINING
TOPIC
•Data types
•Functions
•Scope
•Recursion and Iteration
•File handling
Data types
Data types
QUESTION
Answer: Option D
QUESTION
A. char
B. int
C. float
D. double
Answer: Option B
QUESTION
Determine output:
class A{
public static void main(String args[]){
int x;
x = 10;
if(x == 10){
int y = 20;
System.out.print("x and y: "+ x + " " + y);
y = x*2; } y = 100;
System.out.print("x and y: " + x + " " + y);
}
}
A.10 20 10 100
B. 10 20 10 20
C. 10 20 10 10
D. Error
Answer: Option D
Data types mcq
A.Prints true
B. Prints false
C. Will not compile as boolean is not initialized
D. Will not compile as boolean can never be static
Answer: Option B
Data types mcq
Determine output:
public class Test {
static void test(float x){
System.out.print("float");
}
static void test(double x){
System.out.print("double");
}
public static void main(String[] args){
test(99.9);
} }
A. float
B. double
C. Compilation Error
D. Exception is thrown at runtime
Answer: Option B
QUESTION
Determine output:
public class Test{
int i=8;
int j=9;
public static void main(String[] args){
add();
}
public static void add(){
int k = i+j;
System.out.println(k);
} }
A. 17
B. 0
C. Compilation fails with an error at line 5
D. Compilation fails with an error at line 8
Answer: Option D
QUESTION
Determine output:
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
B. 258 326 326
C. 2 325 69
D. Error
Answer: Option C
QUESTION
Determine output:
public class Test{
public static void main(String[] args){
int i = 010; int j = 07;
System.out.println(i);
System.out.println(j);
} }
A. 8 7
B. 10 7
C. Compilation fails with an error at line 3
D. Compilation fails with an error at line 5
Answer: Option A
QUESTION
Answer: C
QUESTION
Determine output:
public class Test{
public static void main(String[] args){
double STATIC = (char) 2.5 ;
System.out.println( STATIC );
} }
A. Prints 2.0
B. Rraises an error as STATIC is used as a variable which is a keyword
C. Raises an exception
D. Prints ASCII value
Answer: Option A
QUESTION
QUESTION
QUESTION
class Util
{
private static final String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static void recur(int[] digits, int i, String str)
{
if (i == digits.length){
System.out.println(str);
return;
}
int sum = 0;
for (int j = i; j <= Integer.min(i + 1, digits.length - 1); j++) {
sum = (sum * 10) + digits[j];
if (sum > 0 && sum <= 26) {
recur(digits, j + 1, str + alphabet.charAt(sum - 1));
} } }
QUESTION
public class First_C {
public void myMethod() public static void main(String[] args) {
{ First_C c = new First_C();
System.out.println("Method"); c.First_C();
} c.myMethod();
}
{ }
System.out.println(" Instance Block");
}
public void First_C()
{
System.out.println("Constructor ");
}
static {
System.out.println("static block");
}
QUESTION
Answer: A
QUESTION
A. char ch = '\utea';
B. char ca = 'tea';
C. char cr = \u0223;
D. char cc = '\itea';
Answer: A
QUESTION
A. 24
B. 23
C. 20
D. 25
Answer: A
QUESTION
QUESTION
class Util
{
public static int trap(int[] bars){ int right = Integer.MIN_VALUE;
int n = bars.length; for (int i = n - 2; i >= 1; i--) {
int water = 0; right = Integer.max(right, bars[i + 1]);
int[] left = new int[n-1]; if (Integer.min(left[i], right) > bars[i]) {
= Integer.MIN_VALUE;
left[0] water += Integer.min(left[i], right) - bars[i];
for (int i = 1; i < n - 1; i++) } }
{ return water; }
left[i] = Integer.max(left[i - 1], bars[i - 1]); public static void main(String[] args)
} {
int[] right = new int[n]; int[] heights = { 7, 0, 4, 2, 5, 0, 6, 4, 0, 5 };
right[n - 1] = Integer.MIN_VALUE;
for (int i = n - 2; i >= 0; i--) { System.out.print("Maximum amount of water
right[i] = Integer.max(right[i + 1], bars[i + 1]); that can be trapped is " +trap(heights));
} }
for (int i = 1; i < n - 1; i++) { }
if (Integer.min(left[i], right[i]) > bars[i]) {
water += Integer.min(left[i], right[i]) - bars[i];
} }
QUESTION
B. Used for a non-whitespace character
C. Used for a word character
D. Used for a non-word character
Answer: (c)
QUESTION
public class Test2 {
public static void main(String[] args) {
StringBuffer s1 = new StringBuffer("Complete");
s1.setCharAt(1,'i');
s1.setCharAt(7,'d');
System.out.println(s1);
}
}
QUESTION
A. object
B. strictfp
C. main
D. system
QUESTION
A. java.util package
B. java.lang package
C. java.awt package
D. java.io package
QUESTION
Class Main {
static int MAX = 100;
static void printPrincipalDiagonal(int mat[][], int n)
{
System.out.print("Principal Diagonal: ");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// Condition for principal diagonal
if (i == j) {
System.out.print(mat[i][j] + ", ");
}
}
}
System.out.println("");
}
QUESTION
s += z;
System.out.println("" + x + y + s);
}
}
QUESTION
s += z;
System.out.println("" + x + y + s);
}
}
Input:
The input consists of a single line of (m+2) comma-separated integers.
The first number is m, the number of rounds. The next m numbers are ki which
represent the number of piles in each round.
The last number in the input is N, the position in the final pile whose value is to be
determined.
Output:
One integer representing the Nth card after all rounds have been played.
Constraints: Number of rounds <= 10, number of piles in each round <= 13.
Example 1 Input: 2, 2, 2, 4
Output: 13
Example 2 Input: 3, 2, 2, 3, 2
Output: 13
Explanation:
m = 2, k1 = 2, k2 = 2 and N = 4.
We have two rounds. The first round has two piles. At the end of the round, the deck is in the
following order: 1, 3, 5, , 99, 2, 4, 6, , 100
The next round also has 2 piles and after the second round, the cards are in the order 1, 5, 9,
13, .
The fourth card from the top has number 13.
Explanation:
m = 3, k1 = 2, k2 = 2, k3 = 3 and N = 2.
After the second round, the cards are in the order 1, 5, 9, 13,
The third round has 3 piles. Thus after this round the cards will be in the order 1, 13, . the
Second card is 13.
#include<stdio.h> while(count < m) {
int main() { int r = pile[count];
int m; int c = 100 / pile[count];
scanf("%d", &m); if(pile[count] % 2 != 0) {
int pile[m]; c = c + 1;
for(int i = 0; i< m; i++) { }
scanf("%d", &pile[i]); int num = 0;
} int x = 0;
int N; int flag = 0;
scanf("%d", &N); for(int i = 0; i < r; i++) {
int numbers[100]; num = x; for(int j = 0; j < c; j++) {
int num = 1; if(j == 0) {
for(int i = 0; i < 100; i++) { arr[i][j] = numbers[num];
numbers[i] = num++; num = num + pile[count];
} }
int arr[100][100]; else {
int count = 0; int val = numbers[num]; num = num +
pile[count];
if(val > 100) {
break; }
else { arr[i][j] = val; } } } x++; }