Difference Between

 

DIFFERENCES

 

         

Question:

Write a difference between unary and binary operators.

 

Answer:

Unary operator works on only one operand while binary operator works on two operand.

Example: increment, decrement, etc. operator are unary operator (a++ , --a) Addition, subtraction, etc. are binary operator. (a+b) (a-b) 

 

 

Question:

Differentiate between if else if and switch statements.

 

Answer:

if statement tests for integer, character, floating-point type or boolean type, whereas switch statement tests only character or integer value. if else if can test for all types of conditions with the help of relational operator and functions, whereas switch only tests for equality.

 

 

Question:

What is the difference between the linear search and the binary search technique?

 

Answer:

Linear search works on both sorted and unsorted data whereas binary search works only on sorted data.

Linear search starts searching in a sequence in one direction whereas binary search starts

searching from middle element and depending upon the value move left or right in two directions.

 

 

Question:

State a difference between call by value and call by reference.

 

Answer:

If we call a method passing a value then it is known as call by value. The changes made in the called method does not affect the value in the calling method.

 

Ex:

 

class demo{

 int value=50;

 void change(int value){

 value=value+100;

 }

    public static void main(String args[]){

   demo ob=new demo();

   System.out.println("before change "+ob.value);

   ob.change(500);

   System.out.println("after change "+ob.value);  }}

 

Output: before change 50

after change 50

 

In case of call by reference changes being done in the called method, affects the original value where object is passed in place of any primitive value.

Ex:

 

class demo{

 int reference=50;

 void change(demo ob){

 op.data=op.data+100;//changes will be in the local variable only

 }

 public static void main(String args[]){

   demo ob=new demo();

   System.out.println("before change "+ob.reference);

   op.change(ob);//passing object

   System.out.println("after change "+op.reference); }   }

 

Output:before change 50

after change 150

 

 

Question:

Differentiate between searching and sorting.

 

Answer:

In searching, we need  to search the specific elements position or index with respect to the given data.

In sorting, we need to put the given data either in ascending or descending order depending upon the requirement.

 

 

Question:

Write a difference between the functions isUpperCase() and toUpperCase().

 

Answer

isUpperCase() and toUpperCase() are Character class functions isUpperCase() method checks if a given character is in uppercase or not and returns true if the provided character is uppercase. Else, it returns false. Whereas toUpperCase() method converts a given character to uppercase. toUpperCase() is also one of the string function.

 

 

Question:

How are private members of a class different from public members?

 

Answer: The public members can be accessed from any anywhere.

The private members can be accessed only by the methods of the same class.

 

 

Question:

State the difference between while and do-while loop.

 

Answer:

·         while checks condition in the beginning of the loop therefore it is an entry control

·         loop whereas do-while checks condition in the end therefore it is an exit control loop.

·          

·         while does not allow even a single statement to run when the condition is false, whereas

·         do-while allow at least one statement to run even if the condition is false.

 

 

Question:

Write one difference between / and % operator.

 

Answer:

/ operator gives the quotient and % gives the remainder for any two given number.

 

 

Question:

Differentiate between constructor and function.

 

Answer:

Constructor has same name as class name, whereas functions have different names

from their class names. Constructors has no return type,not even void, whereas functions always have a return type.

 

 

Question:

What is the difference between the Scanner class functions next() and nextLine()?

 

Answer:

The next() function lets us input a string with a single word from the user, whereas

nextLine() lets us input a string with multiple words from the user.

 

 

Question:

State the difference between == operator and equals() method.

 

Answer:

The == operator is the relational operator that compares two numeric or character values for equality. The equals() function compares the contents of two String values for equality (uppercase and lowercase characters are treated differently)

 

 

Question:

Differentiate between formal parameter and actual parameter.

 

Answer:

Formal parameters appear in the method definition or signature whereas actual parameter appear in the method call statement. Formal parameter takes values from actual parameters. Actual parameters are the parameters which gets its values at the time of function invocation.

 

 

Question:

Give two differences between switch statement and if-else statement.

 

Answer:

if statement tests for integer, character, floating-point type or boolean type, whereas switch statement tests only character or integer value. if else if can test for all types of conditions with the help of relational operator and functions, whereas switch only tests for equality.

 

 

Question:

Differentiate between constructor and function.

 

Answer: Constructor has same name as class name, whereas functions have different names from their class names. Constructors has no return type,not even void, whereas functions always have a return type.

 

 

Question:

What is the difference between a break statement and a continue statement when they occur in a loop?

 

Answer: The break statement is used to exit immediately from a loop or segment. And the continue statement is used to skip  particular iteration in the loop.

 

 

Question:

Write statements to show how finding the length of a character array char ch[] differs from finding the length of a String object str.

 

Answer: int len = ch.length; length variable is use to find out the length of array int len = str.length(); length function is use to find out the length of string

 

 

Question: Give one point of difference between unary and binary operators.

 

Answer: Unary operator works on only one operand while binary operator works on two operand.

Example: increement, decreement , etc. operator are unary operator(a++ , --a)

 Addition, subtraction, etc. are binary operator. (a+b) (a-b) 

 

 

Question: Differentiate between call by value or pass by value and call by reference or pass by reference.

 

Answer:

If we call a method passing a value then it is known as call by value. The changes made in the called method does not affect the value in the calling method.

Ex:

 

class demo{

 int value=50;

 void change(int value){

 value=value+100;

 }

    public static void main(String args[]){

   demo ob=new demo();

   System.out.println("before change "+ob.value);

   ob.change(500);

   System.out.println("after change "+ob.value);  }}

 

Output: before change 50

after change 50

 

In case of call by reference changes being done in the called method, affects the original value where object is passed in place of any primitive value.

Ex:

 

class demo{

 int reference=50;

 void change(demo ob){

 op.data=op.data+100;//changes will be in the local variable only

 }

 public static void main(String args[]){

   demo ob=new demo();

   System.out.println("before change "+ob.reference);

   op.change(ob);//passing object

   System.out.println("after change "+op.reference); }   }

 

Output:before change 50

after change 150

 

 

Question:

Differentiate between public and private modifiers for members of a class.

 

Answer: The public members can be accessed from any anywhere.The private members can be accessed only by the methods of the same class.

 

 

Question:

What is the difference between an object and a class?

 

Answer:  

Object is an unique entity with some state and behavior.

Class is a group of objects that share common properties and methods.

 

 

Question:

State the difference between entry controlled loop and exit controlled loop.

 

Answer:  

An entry control loop checks condition in the beginning of the loop whereas exit control loop checks condition at the end of the loop.

 

Question:

What is the difference between / and % operators?

Answer:

/ operator gives the quotient and % gives the remainder for any two given number.

 

Question:

Write one difference between Linear Search and Binary Search.

Answer:

Linear search works on both sorted and unsorted data whereas binary search works only on

sorted data.

Linear search starts searching in a sequence in one direction whereas binary search starts

searching from middle element and depending upon the value move left or right in two directions.

 

 

Question:

Differentiate between static and non-static data members.

Answer:

The static data member is common for all the objects for a class. The non-static data members are created separately for each object or instance for a given class.

 

Question:

Write the difference between length and length().

 

Answer:

length variable is use to find out the length of array.

Example: int len = ch.length;

length function is use to find out the length of string.

Example int len = str.length();

 

 

Question:

Differentiate between private and protected visibility modifiers.

 

Answer:

The private members can only be accessed by the members of the same class. The protected data members can be accessed by the members of the same class as well as from the sub-classes of other package using inheritance.

 

No comments:

Post a Comment