Sunday, 21 October 2012

TEST 1- PROGRAMMING FUNDAMENTALS



TEST 1

Rewrite the following switch statement using if-else statement and display the result
using appropriate IDE.

switch(number){
case 1:
jLable2.setText(“digits”);
break;
case 10:
jLable2.setText(“Tens”);
break;
case 100:
jLable2.setText(“Hundreds”);
break;
case 1000:
jLable2.setText(“Thousands”);
break;
Page 9 of 133
default:
jLable2.setText(“error”);
break;
}

What will be the output the following code fragment if the value of ch is
(i) a (ii) c (iii) d (iv) b?
switch (ch)
{ case ‘a’: system.out.println(“It is a.”);
case ‘b’: system.out.println(“It is b.”);
case ‘c’: system.out.println(“It is c.”);
break;
case ‘d’: system.out.println(“It is d.”);
break;
default: system.out.println(“Not a ,b,c ,and d.”);
}

Q) Given THE following set of values:
Byte b;
Char ch;
Short sh;
Int intval;
long longval;
float fl;

 identify the datatype of the following expressions:
1)    ‘a’-3;
2)    Intval*longval-ch;
3)    Fl+longval/sh;
Q) Suppose x and y are two double type variables that you want to add as integers and assign it to an integer variable. Construct a java statement for doing so.
Q)Predict output:
Int a=5;int b=10;
If(a>5)
If(b>5)
{
System.out.println(“b”+b);
}
else
System.out.printn(“a”+a);


What is the output of the following program?
int a=1, b=10;
do {if(a++>- -b)
{continue;}
} while (a<5);
System.out.println(“a=” +i+ “and b= “+b);
(A) a=6 and b=5 (B) a=5 and b=5
(C) a=6 and b=5 (D) a=5 and b=6
(E) a=6 and b=6

What is type casting?\
What is coercion?
What are Keywords? Give two examples of keywords available in Java.
Name and explain the usage of any one relational and one logical operator in Java.
What is the difference between = and == operator in java?
Name the two type of selection statement available in Java.
Write the purpose of Switch Statement with the help of an example. Which Java
Statement can be used in place of switch statement? In the switch statement, what
happens if every case fails and there is no default option?
What is the purpose of ‘break’ statement in java?
What is the purpose of ‘continue’ statement in java?
What is the difference between entry controlled and exit controlled loop
Differentiate between while and do while loop of Java giving suitable examples.
Evaluate the following expressions separately in Java. Assume all variable to be
integers and initial value of a as 10 and b as 20
a. x = a++ + b++;
b. x= ++a + ++ b;
c. x= ++a + b++;
d. x = a / b % b;
e. x= b / a % b;
f. x = b + a++ % b;
g. x = a+ b *a * b;

What would be output of the following code segment written in java.
public static void main(String [] args)
{
int a=10, b= 9;
boolean x;
x = a++ == ++b;
System.out.print("x = "+x);
}

Find the output of the following code snippet written in java
public static void main(String []args)
{
int x=10, y=5;
System.out.println(x++);
System.out.println(++y);
System.out.println((x++ + ++y));
System.out.println((++x - y++));
System.out.println((x++) + (++y));
System.out.println((++x)-(y++));
System.out.println((x++) + (++x));
System.out.println("x = "+ X +" y="+ y);
}

Find the output of the following code snippet written in java
public static void main(String []args)
{
long a=78345,s1=0,s2=0,r;
while(a>0)
{
r=a%10;
if (r%4==0)
s1+= r;
else
s2+=r;
a/=10;
}
System.out.println("S1 ="+ s1);
System.out.println("S2 ="+ s2);
}

Find the output of the following code snippet written in java
public static void main(String args[])
{
int no1=2,no2=1,i;
while(no2<=3)
{
i=no1;
while(i<=no2)
{
no1=no1*i;
i++;
}
System.out.println("No1 "+ no1);
System.out.println("No2 "+ no2);
no2++;
}
}

Find the output of the following code snippet written in java
public static void main(String args[])
{
int x=1,y=1,z=1,i=1;
Page 66 of 133
while(y<=3)
{
z=1;
while(z<=y)
{
x=x*i;
i++; z++;
}
System.out.println("x= "+x);
y++;
}
}

Find the output of the following code segment written in JAVA. Assume all necessary
header files have been included and ignore any syntax error if any.
public static void main(String [] args)
{
int i = 3, a=0, b=1, n=5, c;
System.out.print(a+ “ “+b+” “);
while ( i < n)
{
c= a + b;
System.out.print(c+” “);
a = c - a;
b = c;
i++;
}
}

How many times will the following loop get executed and what will be the final value of
the variable I after execution the loop is over.
int I = 5;
do
{
I += 3;
System.out.println("I = "+I);
I = I + 1;
}
while (I >= 9);

Correct the errors in the following program segment written in JAVA. You are just
required to write the corrected code, underlying the corrections made.
Public Static Void Main (String [] args)
{
Integer Nos = 100;
while (Nos => 45)
{
If (Nos % 5 = 0);
Nos+=10;
otherwise
Nos + = 20;
Page 67 of 133
}
}

Convert the following ‘while loop’ into its equivalent ‘for loop without altering the output
of the program.
int x = 100, a = 30;
while ( x>= 10)
{
System.out.println(”New Amount =”+(x + a));
a++;
x - = a;

byte b;
double d= 417.35;
b= (byte) d;
system.out.println(b);
What will be the output of the following program code?
int m = 100;
int n = 300;
while(++m < --n);
System.out.println(m+” “+ n);

What does the following fragment display
String s = “Six:” + 3+ 3;
System.out.println(s);

State the output of the following program:
public static void main(String args[ ])
{
int x = 10;
int y = 15;
System.ou.println((x>y)? 3.14: 3);
}

State the output of the following program:
public static void main(String args[ ])
{
int x = 10;
float y = 10.0;
System.ou.println((x>y)? true: false);
}


CONVERT
switch(fruit)
{
case ‘A’ :
case ‘a’ : System.out.println(“She likes apple.”);
break;
case ‘O’ :
case ‘o’ : System.out.println(“She likes oranges.”);
break;
default : System.out.println(“She has no preference.”);
}

switch(ch){
case'A': b++;
case'B': ++b;
default: b+=2; System.out.print(b);
}


PREDICT OUTPUT:
int i,j;
for(i=1;i<=4;i++)
{for(j=1;j<=i;j++)
System.out.print(""+i);
System.out.println();
}

Write a program to check whether a given number is a palindrome or not.
Write a program to check whther the given number is odd or even. If odd, then produce factorial of that number and if even, produce fibonacci series of that number.
 or
Write a program to check whther a number is odd or even . if odd, produce a table of that number uptil 10. if even, do nothing.

int m=10,n=7;
while(m%n>=0)
{
m++;
n=n+2;
}
How many times the loop will be executed?

Rewrite while into for loop

int a=10;
while(a<=100)
{
system.out.println("hello");
a=a*5;
}

Find error:

int i,j;
for(i=1,j=2;i>=10;i++,j++);
System.print("hello");