## Java Training in Chennai – Data Types, Variables and Operators Puzzles with Answers – Part – I

Here in this blog we are discussing about Java Training in Chennai – Data Types, Variables and Operators Puzzles with answers – Part – I  . Some of them are discussed below .

______________________________________________________________________________

Guess the output:

int Output = 10;

boolean b1 = false;

if((b1 == true) && ((Output += 10) == 20))

{

System.out.println(“We are equal ” + Output);

}

else

{

System.out.println(“Not equal! ” + Output);

}

In the following pieces of code, A and D will compile without any error. True or false?

A: StringBuffer sb1 = “abcd”;

B: Boolean b = new Boolean(“abcd”);

C: byte b = 255;

D: int x = 0x1234;

E: float fl = 1.2;

class Value

{

public int i = 15;

}

public class Test

{

public static void main(String argv[])

{

Test t = new Test();

t.first();

}

public void first()

{

int i = 5;

Value v = new Value();

v.i = 25;

second(v, i);

System.out.println(v.i);

}

public void second(Value v, int i)

{

i = 0;

v.i = 20;

Value val = new Value();

v =  val;

System.out.println(v.i + ” ” + i);

}

}

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));

}

}

Considering the following code, Which variables may be referenced correctly at line 12?

public class Outer

{

public int a = 1;

private int b = 2;

public void method(final int c)

{

int d = 3;

class Inner

{

private void iMethod(int e)

{

}

}

}

}

What is the result of compiling and running the following code?

public class Tester {

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);

}

}

public class Tester {

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((float) 99.9);

}

}

public class Tester {

static void test(float x) {

System.out.print(“float”);

}

public static void main(String[] args) {

test(99.9);

}

}

class Main {

public static void main(String args[]) {

int t;

System.out.println(t);

}

}

class Test {

public static void main(String[] args) {

for(int i = 0; 0; i++)

{

System.out.println(“Hello”);

break;

}

}

}

class Test

{

public static void main(String[] args)

{

Double object = new Double(“2.4”);

int a = object.intValue();

byte b = object.byteValue();

float d = object.floatValue();

double c = object.doubleValue();

System.out.println(a + b + c + d );

}

}

public class NumberSystem{

public static void main(String[] args){

int hexVal = 0x1a;

System.out.println(“Value : ” + hexVal);

}

}

public class NumberSystem{

public static void main(String[] args){

int val = 0b11010;

System.out.println(“Value : ” + val);

}

}

After the below declaration, What is the value of c[50]?

char[] c = new char[100];

What will be the result of calling the following method with an input of 2?

public int adder( int N ){

return 0x100 + N++ ;

}

What happens when you attempt to compile and run the following code?

public class Logic {

static int minusOne = -1 ;

static public void main(String args[] ){

int N = minusOne >> 31 ;

System.out.println(“N = ” + N );

}

}

What is the output of the below?

public int MaskOff( int n ){

return n | 3 ;

}

How many String objects are created in the following code?

String A, B, C ;

A = new String( “1234” ) ;

B = A ;

C = A + B ;

Which of the following versions of initializing a char variable would cause a compiler error? [Check all correct answers.]

char c = – 1 ;

char c = ‘\u00FF’ ;

char c = (char) 4096 ;

char c = 4096L ;

char c = ‘c’ ;

char c = “c” ;

What happens when you try to compile and run the following code?

public class EqualsTest{

public static void main(String args[]){

Long LA = new Long( 7 ) ;

Long LB = new Long( 7 ) ;

if( LA == LB )

System.out.println(“Equal”);

else System.out.println(“Not Equal”);

}

}

public class EqualsTest{

public static void main(String args[]){

char A = ‘\u0005’ ;

if( A == 0x0005L ) {

System.out.println(“Equal”);

}

else {

System.out.println(“Not Equal”);

}

}}

In the following code fragment, you know that the getParameter call may return a null if there is no parameter named size:

int sz ;

public void init(){

sz = 10 ;

String tmp = getParameter(“size”);

if( tmp != null X tmp.equals(“BIG”))

sz = 20 ;

}

What would happen if you tried to compile and run the following code?

public class EqualsTest{

public static void main(String args[]){

Long L = new Long( 7 );

if( L.equals( 7L ))

System.out.println(“Equal”);

else System.out.println(“Not Equal”);

}

}

public class EqualsTest{

public static void main(String args[]){

Object A = new Long( 7 );

Long L = new Long( 7 ) ;

if( A.equals( L ))

System.out.println(“Equal”);

else System.out.println(“Not Equal”);

}

}

class Test{

public static void main(String args[]){

try{

byte x = 7;

byte y = 5;

System.out.println(x | y);

}catch(Exception e){

System.out.println(

“Exception Thrown”);

}//end catch

}//end main()

}//end class definition

class Test{

public static void main(

String args[]){

try{

boolean x = true;

boolean y = false;

System.out.print((x & y) + ” ” +

(x & x));

System.out.print(”  “);

System.out.print(

(x ^ y) + ” ” + (x ^ x) + ” ” +

(y ^ y));

System.out.print(”  “);

System.out.println(

(x | y) + ” ” + (x | x) + ” ” +

(y | y));

}catch(Exception e){

System.out.println(

“Exception Thrown”);

}//end catch

}//end main()

}//end class definition

class Test{

public static void main(String args[]){

try{

byte x = 1;

boolean y = false;

System.out.println((boolean)x & y);

}catch(Exception e){

System.out.println(“Exception Thrown”);

}//end catch

}//end main()

}//end class definition

class Test{

public static void main(String args[]){

String x = new String(“100”);

String y = new String(“100”);

if(x == y)

System.out.println(“Equal”);

else

System.out.println(“Not Equal”);

}//end main()

}//end class definition

class Test{

public static void main(String args[]){

String x = new String(“100”);

String y = new String(“100”);

if(x.equals(y))       System.out.println(“Equal”);

else       System.out.println(“Not Equal”);

}//end main()

}//end class definition

class Test{

public static void main(String args[]){

String x = “100”;

String y = “100”;

if(x == y)       System.out.println(“Equal”);

else       System.out.println(“Not Equal”);

}//end main()

}//end class definition

What value is assigned to x as the result of executing the following code?

int y = 3;

y++;

x = y++;

What will be the output of the following program?

public class ShortTest {

public static void main(String[] args) {

boolean x = true;

boolean y = false;

if (x || y) { System.out.println(true);

} else { System.out.println(false);

}

}

}

class OperatorsOutput

{

public static void main(String s[])

{

int a = 12 + 21 * 3 – 9 / 2;

int b = 14 – 32 * 4 + 175 / 8 – 3;

if(++a > 71 && –b < 20)

{

System.out.println(“a = ” + a + ” b = ” + b);

}

if(b– == -97 || a– < 100)

{

System.out.println(“a = ” + a + ” b = ” + b);

}

}

}

public class Doubt {

public static void main(String s[]) {

int x = 20;

int y = 25;

if (++x < (y = y -= 4) || (x = x += 4) > y) {

System.out.println(x + “,” + y);

}

}

}

public class DemoOnFloat

{

public static void main(String[] args)

{

float fl = 5.3f;

if (fl == 5.3)

System.out.println(“Both are equal”);

else

System.out.println(“Both are not equal”);

}

}

class Animals

{

public static void main(String [] args)

{

boolean rabbit = true;

boolean donkey = false;

boolean leporidae = true;

if (rabbit & donkey | donkey & leporidae | donkey)

System.out.print(“DOG “);

if (rabbit & donkey | donkey & leporidae | donkey | rabbit)

System.out.println(“CAT “);

}

}

class PrintRelation

{

public static void main(String s[])

{

int a = 7 * 3 + 6 / 2 – 5;

int b = 21 – 8 + a % 3 * 11;

if(a < b)

{

System.out.println(“A is less than B”);

}

if(a = b)

{

System.out.println(“A is equal to B”);

}

if(a > b)

{

System.out.println(“A is greater than B”);

}

}

}

class MyClass1

{

public static void main(String s[])

{

boolean a, b, c;

a = b = c = true;

if( !a || ( b && c ) )

{             System.out.println(“If executed”);         }

Else         {             System.out.println(“else executed”);

}

}

}

public class Blue {

public static void main(String s[]) {

int x = 20;

int y = 25;

if (++x < (y = y -= 4) || (x = x += 4) > y) {

System.out.println(x + ” ” + y);

}

}

}

class StudentPass

{

public static void main(String s[])

{

int marks = 80;

if( marks > 70 )

System.out.println(“Distinction”);

if( marks > 35 )

System.out.println(“Pass”);

else

System.out.println(“Fail”);

System.out.println(“Better luck next time”);

}

}

class FindWinner2

{

public static void main(String s[])

{

int india_score = 300;

int pakistan_score = 290;

System.out.println( india_score > pakistan_score ? “India Wins” : “Pakistan Wins”);

}

}