Professional Documents
Culture Documents
Solution 3RD PB'22
Solution 3RD PB'22
SOLUTION AND
[ COMMON ERRORS ]
Q 1.
import java.util.*;
class dig
{
public static void main()
{ Scanner sc= new Scanner(System.in);
System.out.println("Enter a string");
String s=sc.next();
S=s.toUpperCase(); // [ ERRORS : WRONG FORMAT used for character and String
For String use s.toUpperCase() For character Character.toUpperCase(ch)
int l =s.length();
if(s.charAt(0)==s.charAt(l-1))
}
}
Q 2.
import java.util.*;
class binarysearch
{
public static void main()
{
int i;
for(i=0;i<A.length; i++)
{
System.out.println("Enter the value in ascending order for index no"+i); //position is (i+1)
A[i] = sc.nextInt();
int n = sc.nextInt();
int b=0;
int e=A.length-1;
int m;
int flag=0;
while(b<=e)
{
m=(b+e)/2;
if(n>A[m]) // [ERROR INSTEAD OF A[m] ONLY A OR m USED]
b=m+1;
//[ ERRORS : WRONG VARIABLE AND + OR – USED IN THE WRONG PLACE < > WRONG USE ]
else if(n<A[m])
e=m-1;
else // (n==A[m])
{
System.out.println(n+"found at index no"+m);
flag++;
break;
}// while
if(flag==0) // [ERROR : THIS STATEMENT WAS PUT INSIDE LOOP)
}
}
Q3.
import java.util.*;
} // loop
If(c>0)
System.out.println(“ TOTAL UPPERCASE LETTERS FOUND IN THE ARRAYS “ + c);
else
System.out.println(“ NO UPPERCASE LETTER FOUND IN THE ARRAY”);
}}
Q4.
import java.util.*;
System.out.println(A[i]+" is stored at index no "+ i+ "it's length is "+ A[i].length()); //// [ ERROR
String array each element length A [i].length() ]
}
}}
Q5.
import java.util.*;
class max
{
int i;
double players[]=new double [11]; // array subscripted variable
double s=0.0;
double av=0.0;
for(i=0;i<players.length; i++)
players[i] = sc.nextDouble();
s=s+players[i];
}
double max= players[0]; // [ ERRORS : DECLARE AFTER CREATION OF ARRAY OTHERWISE ARRAY IS
EMPTY]
}}
Q6.
import j ava.util.*;
class palin
{
public static void main()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter a String");
String s=sc.next();
else
System.out.println(" String is not palindrome "+ns);
}
}