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

Java Output and Expression Evaluation

The document contains a series of programming questions related to Java, focusing on output based questions, method behavior, string manipulation, mathematical functions, and control flow. Each question requires the reader to analyze code snippets and predict their outputs, as well as to evaluate expressions and write Java code for specific scenarios. The questions are designed to test understanding of Java syntax, data types, and logic.

Uploaded by

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

Java Output and Expression Evaluation

The document contains a series of programming questions related to Java, focusing on output based questions, method behavior, string manipulation, mathematical functions, and control flow. Each question requires the reader to analyze code snippets and predict their outputs, as well as to evaluate expressions and write Java code for specific scenarios. The questions are designed to test understanding of Java syntax, data types, and logic.

Uploaded by

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

Ouput Based Questions

Q1. What will be the output of the following method for x(5,8)
public void x(int a, int b)
{
a=a+b;
b=a-b;
a=a-b;
[Link](a);
[Link](b);
}
What is the method computing/solving? What names you give to the method x() so that it’s
name become mnemonic.

Q2. Given the following declarations:


String a=”abc”;
String s=a;
String t;
What is the value of the following expression (assume that there is no error)?
a) [Link]();
b) 1+a;
c) “Tomorrow”.indexOf(‘r’);
d) “Tomorrow”.substring(2,4);
e) ([Link]()+a).startsWith(“a”);
f) [Link](1,3).equals(“bc”);

Q3. What is the output of the following program? [2]


public class check
{
public static void change(String nm)
{
nm = “Kush”;
}
public static void main()
{
String name = “Sandeep”;
[Link](name);
change(name);
[Link](name);
}
}

Q4. What will be the output of the following function? [4]


i) [Link](3,0.5) ii) [Link](-21,-12)
iii) [Link](5.2) iv) [Link](2.9)

Q5. Evaluate the following expression: when a=10, b=8 [3]


i) ++a-b– ii) a%b++
iii) a*=b+5
Q6. Write the java expressions for each of the following: [4]
(i) x = e|2x – 4x|
(ii) s=`/sin a + tan-1 a – e2x

Q. 7. Find the output of the following questions. Give the dry run or working. In one line say
what is being computed by the function. [10]

public class output2


{ // if the value of n is 5
public static int unknown(int n)
{
int i,k;
if(n%2==0)
{
i=n/2;
k=1;
}
else
{
k=n;
n–;
i=n/2;
}
while(i>0)
{
k=k*i*n;
i–;
n–;
}
return(k);
}
}

Q8.

public class output3


{
public static void main()
{
int n=5732,result=0;
do
{
result *= 10;
int digit = n%10;
result += digit ;
n = n/10;
}while(n>0);
[Link](“Output = ” + result);
}
}
Q9. Find out the output of each of the following programs. Dry run or working is necessary,
(if possible).

public static void main()


{
String x = new String(“AMIT”);
char c; String p=””; int i;
for(i=0;i<[Link]();i++)
{
c=[Link](i);
p=c+p;
[Link](p);
}
}

Q10. Give the output of the following program segment.

public static void abc()


{
int x=1, i=2;
do
{
x*=i;
}while(++i<=5);
[Link](x);
}

Q11. Give the output of the following program segment:

public class t100


{
public static void main()
{
int i=1,n=6,f=1;
while(i<=n)
{
f=f*i; i++;
[Link](f+” “);
}
}
}

Q12. Give the output of the following program segment:

public class t200


{
public static void main()
{
int i,n=5,s=0;
double f=0;
for(i=n;i>0;i–)
{
s=i*i;
f=([Link](s,2))-i;
[Link](f);
}
}
}

Q13. Give the output of the following program segment [3]


String x = new String(“LOYOLA”);
char c; String p=””; int i;
for(i=0;i<[Link]();i++)
{
c=[Link](i);
p=c+p;
[Link](p);
}

Q14. Give the output of the following program segment [4]

public class q4
{
public static void main()
{
String s=”Object”;
int l=[Link]();
for(int c=0;c<l;c++)
{
if([Link]([Link](c)))
[Link]([Link]([Link](c)));
else if(c%22==0)
[Link](‘E’);
else
[Link]([Link]([Link](c)));
}
}
}

Q15.

public class q1 [4]


{
public static void main()
{
int x[]={60,50,30,40},y=3,size=4;
for(int i=size-1;i>=0;i–)
switch(i)
{
case 0:
case 2: [Link](y*x[i]);break;
case 1:
case 3: [Link](y+x[i]);
}
}
}

Q16. If m=5 and n=2 output the values of m and n after execution in (i) and (ii). [ICSE
2005]

(i) m -= n;

(ii) n = m + m/n;

Q17. What will be the output of the following. if x=5 initially? [ICSE 2005]
(i) 5 * ++x
(ii) 5 * x++

Q18. What is the output of the following? [ICSE 2005]


char c = ‘A’;
short m = 26;
int n = c + m;
[Link](n);

Q19. What do the following functions return for: [ICSE 2005]


String x = “hello”;
String y = “world”;
(i) [Link]((x + y);
(ii) [Link](([Link]());
(iii) [Link](([Link](3));
(iv) [Link](([Link](y));

Q20.

int val=500;
int sum;
n=550;
sum=n+val>1750?400:200;

[Link]("sum is =" +sum);

Q21.

val=1600;
sum=n+val>1750?400:200; //ICSE 2006
[Link]("sum is =" +sum);
Q22.

a=2;
b=3;
c=9;
int out=a-(b++)*(--c); //ICSE 2007
[Link]("OUTPUT:" +out);

Q23.

a=2;
b=3;
c=9;
out=a*(++b)%c; //ICSE 2007
[Link]("OUTPUT:" +out);

Common questions

Powered by AI

The for-loop reverses `"LOYOLA"` by inserting each character at the start of an accumulating string `p`. This reverses the sequence letter-by-letter: Outputs are `"L"`, `"OL"`, `"YOL"`, `"OYOL"`, `"LOYOL"`, and finally `"ALOYOL"`. This order derives from prepending each character, demonstrating character concatenation and dynamic string building.

The expression `Math.pow(3,0.5)` computes the square root of 3. This showcases Java's ability to handle mathematical operations such as exponentiation. The result is approximately `1.7320508075688772`, which is significant in calculating roots of non-perfect squares and is often used in various computations requiring precision.

The program uses a `do-while` loop to reverse the digits of the number `5732`. It successively takes each last digit using `n%10`, appends it to `result`, and reduces `n` by trimming the last digit. This continues until all digits are processed. Therefore, the output is `2375`.

The ternary operator evaluates the condition `(n + val > 1750)`, which simplifies to `1050` in this scenario. The condition yields `false`, so the else part of the ternary `200` is assigned to `sum`. This demonstrates the use of ternary operators for succinct decision-making in assignments.

The `switch` statement iterates in reverse over the array `x={60,50,30,40}`. For index 3 and 1, it adds `y=3` to the element, printing `43` and `53`. For indexes 2 and 0, it multiplies the element by `3`, resulting in outputs of `90` and `180`. Each scenario controls output through designated computational logic defined by the cases.

For `n=5`, the method `unknown(int n)` alternates its logic based on whether `n` is even or odd. Here, `n` is odd, so `k` is initially set to `n` and decremented. The program then iteratively multiplies `k` by the product of `i` and `n`. It calculates `k` as `480`. The function in essence performs a specific calculation that is akin to factorial-like operation with additional multiplicative adjustments based on the parity of `n`.

The method `x(int a, int b)` swaps the two integer values without using a temporary variable. For `x(5,8)`, the swaps proceed as follows: `a` becomes 13 (`a+b`), `b` becomes 5 (`a-b`), and finally `a` becomes 8 (`a-b`). Thus, the output is `85`. A mnemonic name for this method could be `swapValues`.

The result of the program shows that the original string `name` remains unchanged and prints 'Sandeep' both times. This is because Java strings are immutable. The method `change(String nm)` attempts to modify the string, but since strings are immutable, `nm` is only a reference change within the method scope and doesn't affect the original `name` variable outside of it.

The program alters each character's case in the string `"Object"`. If a character is lowercase, it converts it to uppercase; otherwise, it uses a modulus operation to conditionally print 'E'. The flaw is that the modulus condition `c%22==0` is effectively unreachable for a string length of 6, making the condition practically irrelevant. Therefore, characters are printed in lowercase except for converting lowercase to uppercase.

The expression `++a-b--` demonstrates both pre-increment and post-decrement operations. The operations proceed as follows: `a` is incremented to `11` before the subtraction, `b` is used as `8`, then decremented after the subtraction resulting in `7`. The expression evaluates to `3` (`11 - 8`). This shows operator precedence and the effect of increment/decrement alterations of values within expressions.

You might also like