[Go to site: main page, start]

0% found this document useful (0 votes)
14 views6 pages

Java Program Output Analysis

Uploaded by

monika
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

Java Program Output Analysis

Uploaded by

monika
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Which of the following are legal lines of Java code?

1. int w = (int)888.8;
2. byte x = (byte)100L;
3. long y = (byte)100;
4. byte z = (byte)100L;
1 point
a) 1 and 2
b) 2 and 3
c) 3 and 4
d) All statements are correct
What one of the following is best practice to handle Null Pointer exception?
i) int noOfStudents = [Link]().count;
ii) int noOfStudents = getCountOfStudents(line);

public int getCountOfStudents(List line)


{
if(line != null)
{
if([Link]() != null)
{
return [Link]().size();
}
}
throw new NullPointerException("List is empty");
}
1 point
a) Option (i)
b) Option (ii)
c) Compilation Error
d) Option (ii) gives incorrect result
Which is better in terms of performance for iterating an array?
1 point
a) for(int i=0; i<100; i++)
b) for(int i=99; i>=0; i–)
c) for(int i=100; i<0; i++)
d) for(int i=99; i>0; i++)
What will be the output of the following Java program?

class selection_statements
{
public static void main(String args[])
{
int var1 = 5;
int var2 = 6;
if ((var2 = 1) == var1)
[Link](var2);
else
[Link](++var2);
}
}

1 point
a) 1
b) 2
c) 3
d) 4
What will be the output of the following Java program?
class jump_statments
{
public static void main(String args[])
{
int x = 2;
int y = 0;
for ( ; y < 10; ++y)
{
if (y % x == 0)
continue;
else if (y == 8)
break;
else
[Link](y + " ");
}
}
1 point
a) 1 3 5 7
b) 2 4 6 8
c) 1 3 5 7 9
d) 1 2 3 4 5 6 7 8 9
What will be the output of the following Java program?

class Output
{
public static void main(String args[])
{
final int a=10,b=20;
while(a<b)
{

[Link]("Hello");
}
[Link]("World");

}
}
1 point
a) Hello
b) run time error
c) Hello world
d) compile time error
What will be the output of the following Java program?

class Output
{
public static void main(String args[])
{
int a = 5;
int b = 10;
first:
{
second:
{
third:
{
if (a == b >> 1)
break second;
}
[Link](a);
}
[Link](b);
}
}
}
1 point
a) 5 10
b) 10 5
c) 5
d) 10
Which of the below is false about java coding?
1 point
a) variable names should be short
b) variable names should be such that they avoid ambiguity
c) test case method names should be created as english sentences without spaces
d) class constants should be used when we want to share data between class methods
Which of these is long data type literal?
1 point
a) 0x99fffL
b) ABCDEFG
c) 0x99fffa
d) 99671246
What will be the output of the following Java program?

class evaluate
{
public static void main(String args[])
{
int a[] = {1,2,3,4,5};
int d[] = a;
int sum = 0;
for (int j = 0; j < 3; ++j)
sum += (a[j] * d[j + 1]) + (a[j + 1] * d[j]);
[Link](sum);
}
}
1 point
a) 38
b) 39
c) 40
d) 41
Which of these statements are incorrect?
1 point
a) The left shift operator, <<, shifts all of the bits in a value to the left
specified number of times
b) The right shift operator, >>, shifts all of the bits in a value to the right
specified number of times
c) The left shift operator can be used as an alternative to multiplying by 2
d) The right shift operator automatically fills the higher order bits with 0
What will be the output of the following Java program?

class bitwise_operator
{
public static void main(String args[])
{
int var1 = 42;
int var2 = ~var1;
[Link](var1 + " " + var2);
}
}
1 point
a) 42 42
b) 43 43
c) 42 -43
d) 42 43
What will be the output of the following Java program?

class array_output
{
public static void main(String args[])
{
int array_variable [] = new int[10];
for (int i = 0; i < 10; ++i) {
array_variable[i] = i/2;
array_variable[i]++;
[Link](array_variable[i] + " ");
i++;
}

}
}
1 point
a) 0 2 4 6 8
b) 1 2 3 4 5
c) 0 1 2 3 4 5 6 7 8 9
d) 1 2 3 4 5 6 7 8 9 10
What will be the output of the following Java program?

class variable_scope
{
public static void main(String args[])
{
int x;
x = 5;
{
int y = 6;
[Link](x + " " + y);
}
[Link](x + " " + y);
}
}
1 point
a) 5 6 5 6
b) 5 6 5
c) Runtime error
d) Compilation error
What will be the output of the following Java program?

class bitwise_operator
{
public static void main(String args[])
{
int a = 3;
int b = 6;
int c = a | b;
int d = a & b;
[Link](c + " " + d);
}
}
1 point
a) 7 2
b) 7 7
c) 7 5
d) 5 2
What will be the output of the following Java program?

class leftshift_operator
{
public static void main(String args[])
{
byte x = 64;
int i;
byte y;
i = x << 2;
y = (byte) (x << 2)
[Link](i + " " + y);
}
}
1 point
a) 0 64
b) 64 0
c) 0 256
d) 256 0
What will be the output of the following Java program?

class Output
{
public static void main(String args[])
{
int a = 1;
int b = 2;
int c = 3;
a |= 4;
b >>= 1;
c <<= 1;
a ^= c;
[Link](a + " " + b + " " + c);
}
}
1 point
a) 3 1 6
b) 2 2 3
c) 2 3 4
d) 3 3 6
Which of these statements is correct?
1 point
a) true and false are numeric values 1 and 0
b) true and false are numeric values 0 and 1
c) true is any non zero value and false is 0
d) true and false are non numeric values
What will be the output of the following Java code?

class Relational_operator
{
public static void main(String args[])
{
int var1 = 5;
int var2 = 6;
[Link](var1 > var2);
}
}
1 point
a) 1
b) 0
c) true
d) false
What will be the output of the following Java code?

class bool_operator
{
public static void main(String args[])
{
boolean a = true;
boolean b = !true;
boolean c = a | b;
boolean d = a & b;
boolean e = d ? b : c;
[Link](d + " " + e);
}
}
1 point
a) false false
b) true ture
c) true false
d) false true

Common questions

Powered by AI

Option (ii) is considered best practice for handling Null Pointer exceptions. It involves using a method getCountOfStudents(List line) to check if the list is null before attempting to access the list content. This approach encapsulates the null check logic and provides a cleaner, more maintainable code structure compared to inline checks .

The correct statement is d) 'true and false are non numeric values.' In Java, boolean values are treated distinctly from numeric representations and are specifically intended for logical operations .

Yes, '0x99fffL' is a long data type literal. The suffix 'L' is specifically used to denote a literal as a long data type in Java, and the '0x' indicates a hexadecimal number .

For performance, using a for-loop in increasing order (for(int i=0; i<100; i++)) is generally better than in decreasing order (for(int i=99; i>=0; i--)) because of modern CPU caching mechanisms that are optimized for forward memory access patterns .

The legal Java code lines are: 1. int w = (int)888.8; and 2. byte x = (byte)100L. These involve explicit type casting from a larger type to a smaller type, which can lead to data loss if not handled properly. The understanding of type conversion is crucial for performance optimization and avoiding runtime errors .

The program output is '42 -43'. The bitwise NOT operation (~var1) inverts all bits of the integer value, turning 42 into -43 due to the two's complement binary representation used for integers in Java .

The final result is '3 1 6'. This is calculated by evaluating bitwise OR, shift operations, and XOR in sequence: a |= 4 modifies a to 5; b >>= 1 shifts b to 1; c <<= 1 shifts c to 6; a ^= c performs XOR resulting in a being 3 .

The program exposes the concept that variables declared inside a block ({ }) are not accessible outside of it. The error is a compilation error when trying to access variable 'y' outside its block scope after the closing brace .

In a given program comparing variables var1 and var2, the output is 'false' because the relational operator (var1 > var2) evaluates whether 5 is greater than 6, which is not true .

The output is '1 3 5 7'. The loop uses 'continue' to skip even indices and 'break' to exit at y == 8, allowing only 1, 3, 5, and 7 to be printed .

You might also like