0% found this document useful (0 votes)
487 views46 pages

Technical 2

The document provides a summary of a training topic on data types, functions, scope, recursion and iteration, and file handling in C. It includes example code snippets and questions with multiple choice answers related to these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
487 views46 pages

Technical 2

The document provides a summary of a training topic on data types, functions, scope, recursion and iteration, and file handling in C. It includes example code snippets and questions with multiple choice answers related to these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 46

PUZZLE

TCS
DIGITAL
TRAINING
TOPIC

•Data types
•Functions
•Scope
•Recursion and Iteration
•File handling
Data types
Data types
QUESTION

Comment on the output of this C code?


int main()
{
int a[5] = {1, 2, 3, 4, 5};
int i;
for (i = 0; i < 5; i++)
if ((char)a[i] == '5')
printf("%d\n", a[i]);
else
printf("FAIL\n");
}

A. The compiler will flag an error


B. Program will compile and print the output 5
C. Program will compile and print the ASCII value of 5
D. Program will compile and print FAIL for 5 times

Answer: Option D
QUESTION

The format identifier ‘%i’ is also used for _____ data


type?

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

The following program:


public class Test{
static boolean isOK;
public static void main(String args[]){
System.out.print(isOK);
} }

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

What does the expression float a = 35 / 0 return?


A. 0
B. Not a Number
C. Infinity
D. Run time exception

    
    

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 static void main(String[] args)


{
int[] digits = { 1, 2, 2 };

String str = "";


recur(digits, 0, str);
}
}
Scope of Variables

Modifier Package Subclass World


Public Yes Yes Yes
protected Yes Yes No Default (no
modifier) Yes No No private No
No No
QUESTION

What will be the output of the following program?


public class ScopeOfVariables {
    public static void main(String[] args) {
        int x = 10;
        int y = 20;
        {
            System.out.print(x + ", " + y);
        }
        {
            x = 15;
            System.out.print(" - " + x + ", " + y);
        }
        System.out.print(" - " + x + ", " + y);
    }
}
QUESTION

What will be the output of the following program?


public class Book {
    public static void main(String[] args) {
        String book = "java";
        System.out.println("Book name " + book);
        {
            int pages = 300;
            System.out.println("Pages " + pages);
            double price = 275.50D;
        }
        price = 275;
        System.out.println("Price " + price);
    }
}
QUESTION

What will be the output of the following program?


public class Industries {
    public static void main(String[] args) {
        {
            String name = "TEXMO";
            System.out.println(name + " Industries");
        }
        name = "texmo";
        System.out.println(name + " Industries");
    }
}
QUESTION

What will be the output of the following program?


public class LocalVariable {
public void addition() {
int number ;
number = number + 23;
System.out.println("Recent number is : " + number);
}
public static void main(String args[]) {
LocalVariable test = new LocalVariable();
test.addition();
}
}
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

The \u0021 article referred to as a

A. Unicode escape sequence


B. Octal escape
C. Hexadecimal
D. Line feed

       

    

Answer:  A
QUESTION

Which of the following is a valid declaration of a char?

A. char ch = '\utea';
B. char ca = 'tea';
C. char cr = \u0223;
D. char cc = '\itea';

       

    

Answer:  A
QUESTION

Evaluate the following Java expression, if x=3, y=5, and z=10:


++z + y - y + z + x++

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

What is the use of \w in regex?

   A.    Used for a whitespace character

    
B. Used for a non-whitespace character
C. Used for a word character
D. Used for a non-word character

Answer: (c)
QUESTION

What will be the output of the following program?

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

 Which of the following is a reserved keyword in Java?

A. object
   B.    strictfp
C. main
    
D. system
QUESTION

Which package contains the Random class?

A. java.util package
       
B. java.lang package
    
C. java.awt package
D. java.io package
QUESTION

What will be the output of the following program?


public class Test {  
public static void main(String[] args) {  
    
       int count = 1;  
    while (count <= 15) {  
    
    System.out.println(count % 2 == 1 ? "***" : "+++++");  
    ++count;  
        }      // end while  
    }       // end main   
 }  
QUESTION

Program to print the Diagonals of a Matrix


Given a 2D square matrix, print the Principal and Secondary diagonals.
Examples :
     4 1 2 3 4
   Input:
4321
     7 8 9 6
6543
Output:
Principal Diagonal: 1, 3, 9, 3
Secondary Diagonal: 4, 3, 8, 6
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

static void printSecondaryDiagonal(int public static void main(String args[])


mat[][], int n)     {
    {         int n = 4;
    
           System.out.print("Secondary Diagonal:         int a[][] = { { 1, 2, 3, 4 },
");                       { 5, 6, 7, 8 },
    
                         { 1, 2, 3, 4 },
        for (int i = 0; i < n; i++) {                       { 5, 6, 7, 8 } };
            for (int j = 0; j < n; j++) {   
           printPrincipalDiagonal(a, n);
                // Condition for secondary         printSecondaryDiagonal(a, n);
diagonal     }
                if ((i + j) == (n - 1)) { }
                    System.out.print(mat[i][j] + ",
");
                }
            }
        }
        System.out.println("");
    }
QUESTION

Predict the output of following Java Programs


public class Main
{
        public static void gfg(String s)
{
     System.out.println("String");
}
public static void gfg(Object o)
{
System.out.println("Object");
}

public static void main(String args[])


{
gfg(null);
}
} //end class
QUESTION

Predict the output of following Java Programs


public class Main
{
        public static void main(String args[])
{
     short s = 0;
int x = 07;
int y = 08;
int z = 112345;

s += z;
System.out.println("" + x + y + s);
}
}
QUESTION

Predict the output of following Java Programs


public class Main
{
        public static void main(String args[])
{
     short s = 0;
int x = 07;
int y = 08;
int z = 112345;

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++; }

You might also like