100% found this document useful (1 vote)
25 views2 pages

Short Q2

The document contains multiple programming snippets and questions related to Java, including outputs of code segments, string methods, and error identification. It also covers concepts like OOP principles, data type conversions, and loop execution counts. Additionally, it asks for corrections to faulty code and the implementation of specific functionalities.

Uploaded by

ayanacharya2009
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
100% found this document useful (1 vote)
25 views2 pages

Short Q2

The document contains multiple programming snippets and questions related to Java, including outputs of code segments, string methods, and error identification. It also covers concepts like OOP principles, data type conversions, and loop execution counts. Additionally, it asks for corrections to faulty code and the implementation of specific functionalities.

Uploaded by

ayanacharya2009
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
You are on page 1/ 2

Find the output of the following program snippets: Give the output when p=1; Give the output

ve the output of the following


String s=”45”; int a=1,b=2; program segment and mention
int t=Integer.parseInt(s); switch(p) how many time the loop will
t=t+100; { execute.
System.out.println(t); case 1: a++ + ++a; int a=0, i=4;
case 2: ++b; while(i++ <10)
String s=”123.45”;
break; a=++i + i*2;
Int x=Integer.parseInt(s.substring(0,3)); float
} System.out.println(“a=”+a+”
y=float.parseFloat(s.substring(3));
System.out.println(a+” “+b); “+”i=”+i);
System.out.println(x+y);
State the method that: What will be the output of the following string methods?
a) Converts a string to a primitive float data type a) System.out.println(“All the Best”.length()/2);
b) Determines if the specified character is an upper b) System.out.println(“acquatic”.equalsIgnoreCase(“A
case character. CQUATIC”));
Give the output of the following string functions: Rahul is trying to implement a program in java that
a) “ACHIEVEMENT”.replace(‘E’,’A’); calculates that factorial of a given number using while
b) “DEDICATE”. compareTo(“DEVOTE”); loop. However, his code is not producing the expected
Evaluate the expression when the value of p=5 output. Identify the logical and syntax errors in his
p*=++p + --p/++p +p code and rewrite them to ensure the correct
Name the following a) The return type of compareTo Function calculation of the factorial.
b)A principle of OOP, whicjh allows a method to be Public int factorial(int x)
used for multiple purposes {
long fact=0;
Mention syntax error if there is, else write the output of the for(int i=1;i<=x;i++)
following code: fact=fact*i;
doolean b=false; return fact;
if(b=true) }
Systme.out.println(“b is true”);
else
Systme.out.println(“b is false”);
Give the output of the following code snippet: Write a statement each to perform the following task on a string:
double z[]={0.3,4.5,23.0,4.5} a) find and display the position of the list space in s a single s;
System.out.println(Math.pow((z[1]+z[3]),2)); b)convert a number stored in a string x to double data type.
Systme.out.println(z.length);
What will be the output of the following snippet Give the output of the following code when v=1;
code? int m=10, s=5;
class output switch(v)
{ {
public static void main(Stirng args[]) case 1: m++;
{ case 2: m=m+2;
Integer i=new Integer(514); case 3: m*=3;
flaot x=i.flaotvalue(); case 4: System.out.println(“The value of m=”+m);
System.out.println(x); break;
} case 0:s-=--s+m+ %2+m;
} case -2: System.out.println(“The value of s=”+(s++));
break;
}
Name the operator listed below: a) >= b) && c) % d) != Name the following a) The access specifies that does not
need a keyword b) int [][] mat= { {1,6,9},{4,6,2},{5,3,8}};
Give the value of mat.length and mat[1][1]
Given the output of the following java statement Write the output of the given statement
String[]= s={“LION”,”TIGER”,”ELEPHANT”,”LIONESS”}; String na=”loop structure”;
System.out.println(s[0].compareToIgnoreCase(s.[3])); System.out.println(na.lastIndexOf(na.charAt(8)));
System.out.println(s[2].charAt(s[2].length()- System.out.println(na.replace(na.substring(4),”ing”));
2)==s[3].charAt(s[3].length()/2));
Identify the following java statements as an example of
Write the following the shorthand form? a=a/15 unboxing and autoboxing:
Write the output of the given statement, a)Integer in=687;
System.out.println(“5.0=5L?”+(5.0==5L)); b) Double d=new Double(48.5); double c=d;

What is the result of compiling and running the Write the output of the following code
following program String animal=”unknown”;
class example int weight=700;
{ char sex=’m’;
public void method(int k) double colorWaveLength=1.630;
{ if(weight>=500)
System.out.println(k); animal=”elephant”;
} if(colorWaveLength>1.621)
public int method(int i) animal=Gray”+amimal;
{ if(sex>=’f’)
Systme.out.println(i+1); animal=”female”+animal;
return i+1; Systme.out.println(“The animal is a “+ animal);
}
public static void main(string [] args) Answer the following:
{ a) A keyword used to call a package in the program
example e=new example(); b) what are the two ways of invoking function?
e.method(5); A array with elements 18,13,2,9,5 become 13,2,9,18,5 after three
} consecutive presses, whish sort is being implemented in the above
} array?
Debug the error and rewrite the following function prototype, i) int sum(x,y); ii) flaot operate(int x,flaot 3.5f)
Predict the output int a=5, b=8; a+=a++ + ++a + (b++ + ++a) +a+ b; System.out.println(a);
Sumedh has designed to code to check if the number is Give the output
a prime number of not, He has written the following int a[]={2,4,6,6,19};
code but the code is giving wring output. Rewrite the a[0]=23;
code for the correct output. a[3]=a[1];
boolean flag=false; int c=a[0]+a[3];
for( int i=2;i<=num;i++) Systme.out.println(“Sum=”+c);
{ Sam executes the following program segment and the answer
if(num%i==0) displayed is zero, irrespective of any non-zero value are
flag=true; given. Name the error, How the program can be modified to
} get the correct answer.
if(flag) void triangle(double b, double h)
Syustem.out.println(“prime number”); {
else double a;
System.out.println(“Not a prime number”); a=1/2*b*h;
System.out.println(“Area=”+a);
Rewrite the following program segment using if –else statements instead of the ternary operator:
T=(x+y<z || x+z<=y || y+z<=x)? “Triangle is not possible”:”Triangle is possible”;
Declare and initialized a one dimensional int array named evenNums with five elements. Use an initializer list that
contains the first five even integers, staring with 11 and print 4th elements.

You might also like