Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

Write the purpose of parseInt() method.

Answer»

paraseInt converts the string into a integer.

2.

What is the difference between statements (i) and (ii) ?(i) t = 2;(ii) if (t = = 2)d = 3;

Answer»

(i) t is a variable assigned a value 2.

(ii) It is a conditional statement if t = = 2 true then d = 3.

3.

Identify invalid variable names out of the following. State reason if invalid.(i) for (ii) - salary(iii) salary 12 (iv) product

Answer»

(i) for - keyword.

(ii) salary - cannot start with '-'

4.

What is the difference between setVisible() and setEnabled() methods?

Answer»

setVisible () makes a component visible or invisible while SetEnabled () = enables or disables a component for use.

5.

When is if-else-if statement preferred over switch statement?

Answer»

The If-else can handle a range of comparisons whereas the switch can handle only equality. 

The If-else can combine logical operations.

6.

What will be the contents of jTextField1 and jTextField2 after executing the following code:String s = "Best";String r = "LucK";String z;z = r.concat(s);jTextField1.setText(z);jTextField2.setText(r.toUpperCase());

Answer»

jTextField1 = LuckBest

jTextField2 = LUCK

7.

What will be the value of P and Q after execution of the following code:int P, Q = 100;for(P = 10; P<=12; P++){Q+ = P;}JOptionPane.showMessageDialog(this, "P:" + P + "Q:" + Q +" ");

Answer»

Output

P:13

Q:133

8.

What will be displaced in of jTextField1 after executing the following code?int m = 16;m = m+1;if(m<15)jTextField1.setText(Integer.toString(m));elsejTextField1.setText(Integer.toString(m + 15));

Answer»

The correct output

32

9.

What is the purpose of break statement?

Answer»

The break statement helps to exit the innermost loop.

10.

Rewrite the following code using if... else... if statement:switch (depcode){case 1:       allowance = 4000;       break;case 2:       allowance = 3200;       break;default;       allowance = 1000;}

Answer»

if (depcode ==1)

allowance = 4000;

else if (depcode == 2)

allowance = 3200;

else

allowance = 1000;

11.

Rewrite the following code using if .. else... if statement:switch (month) {case 1: monthString = "January";break;case 2: monthString = "February";break;default; monthString = "Invalid";}

Answer»

if (month==1)

monthString = "January ";

else if (month==2)

monthString = "February";

else

monthString = "Invalid";

12.

Rewrite the following code using while loop:int x = 100;for(int i = 2; i < =22; i = i + 4){jTextArea1.append('\n" + (i +x));x = x-2;}

Answer»

int x = 100;

int i = 2;

while (i<=22)

{

jTextArea1.append("\n" +(i + x));

x = x-2;

i = i + 4;

}

13.

Rewrite the following code using while loop :int a,b;for(a = 10, b = 4; a &lt; =16; a++, b + = 2){jTextArea1.append(" " + a++);}JOptionPane.showMessageDialog(null,"Finished!!!");

Answer»

int a = 10, b = 4;

while(a<=16)

{

jTextArea1.append(" "+a++);

a++;

b+=2;

}

JOptionPane.showMessageDialog(null,"Finished!!!");

14.

Write Java code that takes the cost of a pencil from jTextField1 and number of pencil from jTextField2 and calculate the total amount as cost*number to be displayed in jTextField3 and 20% of the service tax out of total amount in jTextField4.

Answer»

double cost = Double.parseDouble(jTextField1.getText());

int n = Integer.parseInt(jTextField2.getText());

double amount = cost * n;

jTextField3.setText(Double.toString(amount));

jTextField4.setText(Double.toString(amount * 0.20));

15.

Write a Java statement to make the jTextField1 non-editable.

Answer»

jTextField1.setEditable(false);

16.

Rewrite the following program code using a if statement:int C = jcomboBox1.getSelectedlndex();Switch (C){case 0 : Amount=Bill;break;case 1 : Amount=0.9*Bill;break;case 2 : Amount=0.8*Bill;break;default : Amount=Bill}

Answer»

if(C==0) Amount = Bill;

else if (C = =1) Amount = 0.9*Bill;

else if (C = 21) Amount = 0.8*Bill;

else Amount = Bill;

OR

if(C==0) Amount = Bill;

if(C==1) Amount = 0.9*Bill;

if(C==2) Amount = 0.8*Bill;

if ((C>2) | | (C<0)) Amount = Bill;

17.

How many times will the loop execute?int value1 = 7,value2 =19;do{JOptionPane.showMessageDialog(null, value1 + value2);value1 = value1+2;value2 = value2-2;}while(value1 &lt; = value2);

Answer»

4 times will the loop execute.

18.

Rewrite the following Java code using switch case statement:Int option=Integer.parseInt(JTextField1.getText ());if (option==1)JTextField2.setText ("Regular Employee");else if (option==2)JTextField2.setText ("On Probation");else if (option==3)JTextField2.setText("Visiting Faculty");else if (option== 4)JTextField2.setText("On Contract");elseJTextField2.setText("Invalid option");

Answer»

int option = Integer.parseInt(JTextField1.getText ());

switch (option)

{

case 1 : JTextField2.setText("Regular Employee");

break;

case 2: JTextField2.setText("On Probation");

break;

case 3: JTextField2.setText("Visiting Faculty");

break;

case 4: JTextField2.setText ("On Contract");

break;

default: JTextField2.setText ("Invalid option");

}

19.

Rewrite the following program code using IF... else if instead of switch statement.String rem;int code = Integer.parseInt(jTextField1.getText());Switch (code){case 1:rem = "Classes start on 8th April";break;case 2:rem = "Classes start on 10th April";break;case 3:rem = "Classes start on 12th April";break;default :rem = "Contact Admin Office";}

Answer»

String rem;

int code = Integer.parseInt(TextField1.getText());

if (code ==1)

rem = "Classes start on 8th April";

else if (code ==2)

rem = "Classes start on 10th April";

else if(code ==3)

rem = "Classes start on 12th April";

else

rem = "Contact Admin Office";

20.

What is the purpose of break keyword while using switch case statement ? Illustrate with the help of an example.

Answer»

The break statement stops the flow of logic within the switch statement and the statement immediately following the switch is executed.

OR

The break statement prevents "fall through" in the switch statement.

Example:

switch (n)

{

case 10:

System.out.printIn ("Ten") ;

break;

case 20:

System.out.printIn ( "Twenty") ;

break;

default;

System.out.printIn("Not Ten or Twenty") ;

}

21.

"The variable/expression in the switch statement should either evaluate to an integer value or String value." State True or False.

Answer»

True, "The variable/expression in the switch statement should either evaluate to an integer value or String value."

22.

Write Java code that take any three digit number from the user in jTextField1, calculate the sum of the digits and display it in separate textfield named jTextField2.For example, If the number entered is 432, It should answer as 9 (i.e. 4+3+2).

Answer»

int n = Integer.parsefnt (jTextFieId1.getText());

int result = 0;

while (n>0)

{

result = result + n %10;

n = n/10;

} jTextField2.setText(" " +result);

23.

Rewrite the following program code using a switch statement.if(code = = 1)Month = "January";else if(code==2)Month = "February";else if(code= =3)Month = "March";else if(code: =4)Month ="April";elseMonth = "No Match";

Answer»

switch(code)

{

case 1: Month = "January";

break;

case 2;

break;

case 3: Month = "March";

break;

case 4: Month ="April";

break;

default: Month = "No Match";

}

24.

Write one difference between if statement and switch statement.

Answer»

The switch statement must be a single integer control variable, and each case section must correspond to a single constant value for the variable. The if...else..... if combination allows any kind of condition after each if.

25.

The following code has error(s). Rewrite the correct code underlining all the corrections made :int x=10;int y=50;do;{x+5=x:Y-5=Y;while (x&lt;=y);

Answer»

int x =10;

int y=50;

do

{

x=x+5;

y=y-5;

}  while (x<=y);

26.

The following code has error(s). Rewrite the correct code underlining all the corrections made:int a =0;int b=15;do;a = a + 3;b=b-3;while a&lt;=b;

Answer»

int a = 0;

int b = 15;

do

{

a = a+3;

b=b-3;

}  while (a<=b);

27.

The following Code has some error(s). Rewrite the correct code underlining all the corrections made:int written, interview;written = Integer.parseInt (jTextField1.getText () ) ;inteview=Integer . parseInt ( jTextField2.getText ()) ;if (written&lt;80) OR (interview&lt;15){system.out.printiIn (Not selected) ;}Else;{System.out.printIn ("selected");}

Answer»

int written, interview;

written=Integer.parseInt (jTextField1.getText()) ;

interview= Integer.parseInt (jTextField2.getText());

if ( (written<80) | | (Interview<15) )

{

System. out . printiln ("Not selected");

}

else

{

System. out.println ( "selected") ;

}

28.

The following code has error(s). Rewrite the correct code underlining all the corrections made :int n=5int i=1, f=1do;{f=f*i;i++;while(j&lt;=n)jTextField1.setText(" " +f);

Answer»

int n=5, i = 1, f = 1;

do

{

f = f*i;

i++;

}   while(i<=n);

jTextField1.setText(" "+f);

29.

Is a string containing a single character same as a character?

Answer»

No, a string containing a single character same as a character.

30.

Write the difference between the following :(i) A = 10(ii) If (A==10)

Answer»

(i) Assignment operator, It assigns the value 10 to variable A

(ii) Relational operator, It checks if the value of A is equal to 10 or not.

31.

The following code has some error(s). Rewrite the correct code underlining all the correction made:Int k=2;sum = 0;//Declaring k and sum as Integerdo{sum=k; k+=2;}while(k= &lt;20)jTextField1(Integer.tostring(sum));

Answer»

int k = 2; sum = 0; //could also be written as int k = 2; int sum = 0;

do { sum=k; // could also be written as sum = sum + k;

k + = 2;

} while(k< =20);

jTextField1.setText(Integer.toString(sum)) ;

32.

The following code has some error(s). Rewrite the correct code underlining all the corrections made :int z;z =14;do;z = z-2;System.out. displayIn(z);}while z &gt;= 2;

Answer»

int z;

z = 14;

do//no semicolon

{

z = z - 2;

System.out.printIn(z);

} while (z >= 2);

33.

Write statement to increase the value assigned to variable z by 5 and then to display the value.

Answer»

z = z + 15;

System. out.prtintIn(z);

34.

Distinguish between '/ ' and '%' operators.

Answer»

'/ ' divides first number with second number and returns the quotient.

'%' divides first number with second number an returns the remainder.

35.

Differentiate between BACKGROUND and BGCOLOR.

Answer»

BACKGROUND is the image attribute in <BODY> tag where you can place graphic object to make the web pages more attractive. BGCOLOR attribute is used to set the background color of the webpage.

36.

Realizing the importance of the Internet, Ms. Shikha a Mathematics teacher, has decided to use Internet as a medium to teach her students Mathematics in an interesting way.Help her in matching Column A with suitable statements of Column'B'.Column AColumn BXMLWe use predefined tags to design web pages.It is designed to store and transport dataHTMLTag are not case sensitiveXMLHTMLElements must have a closing tag.

Answer»
Column AColumn B
XMLElements must have a closing tag.
It is designed to store and transport dataXML
Tag are not case sensitiveHTML
HTMLWe use predefined tags to design web pages.

37.

What will be displayed in jTextField1 and jTextField2 after the following code is executed :int t;int x;x = 12;t =(3*x+ +)/3;jTextField1. setText("" + t);jTextField2. setText(" " + x) ;

Answer»

TextField1 = 12

jTextField2 = 13

38.

What will be displayed in jTextField1 and jTextField2 after the following code is executed:int t;int s;s = 2;t = (4*s++)/2;jTextField1.setText ("" + t);jTextField2.setText (""+s);

Answer»

4 will be displayed in jTextField1

3 will be displayed in jTextField2

39.

Name the four component of JDBC?

Answer»

JDBC consists of four components: The JDBC API,JDBC DriverManager, JDBC Test Suite and JDBC-ODBC bridge.

40.

Write Java code to assign the value 70 to variable y. Then decrease the value of y by 5 and store it in variable z.

Answer»

y = 70;

z = y - 5;

or  y = 70;

y = y - 5;

z = y;

41.

Zia is working with a list box. She has placed a list control on her form to display the list of all items available in her shop. She wants to allow the user to select multiple items from her list control. Which property of jList should she use to do the same?

Answer»

She should use the Selection Mode property.

42.

What is the importance of java.sql.*; in java jdbc connection?

Answer»

The java.sql.package has to be imported first before we can create a java jdbc connection to the database.

43.

In a Java program, Rajat wants to use a variable to store the quantity of an item which may be in whole numbers or decimals. Write a suitable Java statement to declare the variable for the above mentioned purpose.

Answer»

He can use float or double type od variable. double num;

or

float num;

44.

Aryamani is creating a simple application in Java called "Name Concatenator" in which he needs to concatenate the first name, middle name and last name input by the user in separate text fields. Name a method and an equivalent operator for this purpose.

Answer»

Str.concat(str)

Operator '+' can be used

45.

Tanyaman is creating a simple application In java called "Password Checker" in which she needs to convert the characters input by the user in a particular case. Name two methods of the String class that she can use for this purpose.

Answer»

toLowerCase()

toUpperCase()

46.

Write the name HTML tag used to include numbered list in a HTML Web Page.

Answer»

<OL>

OR

Mentioning ORDERED LIST Tag

47.

Sharnbhavi has to design two web pages with following specifications :(i) One web page should have an unordered list.(ii) Another web page should have background "Yellow" in colour.Suggest her suitable tag(s) and attribute(s) for the above specifications.

Answer»

(i) <UL> <LI>

(ii) <body> tag and bgcolor attribute

48.

People who wish to send flowers to their near and dear ones, contact 'Fragrance Florist'. Ms. Sharma works as a programmer in "Fragrance Florist" where she has designed a software to compute charges to be paid by the customer. A screenshot of the same is shown below:(i) A customer orders for one type of flowers out of Red Rose/OrchidAellow Rose.(ii) A "Special Customer" gets a 50.00 discount on Amount.(iii) Name, Phone Number Flower ordered, whether customer is a Special customer, Number of Stems is input by the user.(iv) Amount, Discount (if any), Amount to pay are calculated and displayed by the program.Amount is calculated using the following criterion :FlowerPrice per Stem (in Rs)Red Rose15 per stemOrchid60 per stemYellow Rose20 per stem(i) Amount is obtained by multiplying per Stem charges with number of stems bought.(ii) If 'Special Customer' checkbox is selected, discount of Rs 50.00 is given on Amount.Amount to Pay = Amount - DiscountHelp Ms. Sharma by writing the code to do the following:(i) After selecting appropriate Radio Button and checkbox (if Spl Customer), when 'Calculate' button is clicked, AMOUNT, DISCOUNT (IF ANY) and AMOUNT TO pay should be calculated and displayed in the respective text fields.(ii) When 'CLEAR button is clicked, all text fields, Radio buttons and checkbox should be cleared.(iii) When 'CLOSE' button is clicked, the application should close.

Answer»

(i) float dis;

int amt, int noofstems = Integer.parseInt(jTextField3.gettext());

if (jRadioButton1.isselected())

amt =15 * noofstems;

else if (jRadioButton2.isselected())

amt = 60 * noofstems;

else if(jRadioButton3.isselected())

amt = 0;

jTextField4.setText(" " + amt);

if(jcheokBox1.isselected())

jTextField5.setText("50.00");

dis = 50.00;

}

float amountPay = amt - dis;

jTextField6.setText(" "); jTextField2.setText (" ");

jTextField3.setText(" "); jTextField4.setText (" ");

j TextFietd5.setText(" " ); jTextField6.setText (" ") ;

jRadioButton1.setSelected (false);

jRadioButton2.setSelected (false);

jRadioButton3.setSelected (false);

jRadioButton1.setSelected (false);

Note : NULL in place of " " should also be accepted

(iii) System.exit(0);

49.

Write the output:System.out.printIn(Math.pow(4.0,2.0)) ;System.out.printIn(Math.round(6.459));

Answer»

Output

16.0

6.5

50.

What will be the value of X1 after executing of the following code:String X1 = "Spread", X2 = "PEACE";X1 = X2.concat (X1);

Answer»

Output

PEACESpread