Computer Extra Questions
Computer Extra Questions
An infinite loop is a sequence of instructions in a computer program which loops endlessly, either due to the loop having no terminating condition,
having one that can never be met, or one that causes the loop to start over.
The break statement cannot be used independently in the Java program, i.e., it can only be written inside the loop or switch statement.
Consider the following example in which we have used the break statement with the for loop.
BreakExample.java
Unlike break statement, the continue statement doesn't break the loop, whereas, it skips the specific part of the loop and jumps to the next
iteration of the loop immediately.
Consider the following example to understand the functioning of the continue statement in Java.
Encapsulation in Java refers to integrating data (variables) and code (methods) into a single unit. In encapsulation,
a class's variables are hidden from other classes and can only be accessed by the methods of the class in which
they are found.
A class that is derived from another class is called a subclass (also a derived class, extended class, or child class).
The class from which the subclass is derived is called a superclass (also a base class or a parent class).
Example
/**
* This method is declared as protected.
*/
protected String getInfo() {
return name +" is "+ age +" years old.";
}
public static void main(String[] args) {
System.out.println(new ProtectedTest().getInfo());
}
}
/**
* This method is declared with default aacees specifier
*/
String getInfo() {
return name +" is "+ age +" years old.";
}
public static void main(String[] args) {
System.out.println(new DefaultTest().getInfo());
}
}
Following are the notable differences between Class (static) and instance variables.
Example
Live Demo
public class VariableExample{
int myVariable;
static int data = 30;
public static void main(String args[]){
VariableExample obj = new VariableExample();
System.out.println("Value of instance variable: "+obj.myVariable);
System.out.println("Value of static variable: "+VariableExample.data);
}
}
Following are the notable differences between Class (static) and instance variables.
Instance variables Static (class) variables
Instance variables hold values that There would only be one copy
must be referenced by more than of each class variable per
one method, constructor or block, or class, regardless of how many
essential parts of an object's state objects are created from it.
that must be present throughout the
class.
Example3
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object.
It is an important part of OOPs (Object Oriented programming system). The idea behind inheritance in Java is that
you can create new classes that are built upon existing classes.
The error message "subscript out of bounds" means that it's looking for an index that doesn't exist. Since there is no
row below the last row, there's no row for it to "down select". You need some logic that tells it what to do if the
column number is equal to the number of rows in the matrix.
The key difference between Java's length variable and Java's length() method is that the Java length variable
describes the size of an array, while Java's length() method tells you how many characters a text String contains.
Linear Search Binary Search
Commonly known as sequential search. Commonly known as half-interval search.
Elements are searched in a sequential manner (one by one). Elements are searched using the divide-and-conquer approach.
The elements in the array can be in random order. Elements in the array need to be in sorted order.
Less Complex to implement. More Complex to implement.
Linear search is a slow process. Binary search is comparatively faster.
Single and Multidimensional arrays can be used. Only single dimensional array can be used.
Does not Efficient for larger arrays. Efficient for larger arrays.
The worst-case time complexity is O(n). The worst case time complexity is O(log n).
Package in JAVA is a mechanism to encapsulate a group of classes, sub packages and interfaces.
Import Package
The wrapper class in Java provides the mechanism to convert primitive into object and object into primitive.
compareTo() equals()
The result is a negative, positive or zero integer value depending on whether the String object precedes, The result is true if the contents are same
follows or is equal to the String argument otherwise it is false.
StringBuffer
String
2) String is slow and consumes more memory when we concatenate too StringBuffer is fast and consumes less
many strings because every time it creates new instance. memory when we concatenate t strings.
3) String class overrides the equals() method of Object class. So you can StringBuffer class doesn't override the
compare the contents of two strings by equals() method. equals() method of Object class.
4) String class is slower while performing concatenation operation. StringBuffer class is faster while performing
concatenation operation.
5) String class uses String constant pool. StringBuffer uses Heap memory
Equals ignore case is not case sensitive whereas equals() is case sensitive
For example, a missing semicolon at the end of a line or an extra bracket at the end of a
function may produce a syntax error. A logic error (or logical error) is a 'bug' or mistake in a
program's source code that results in incorrect or unexpected behaviour.
Dividing a number by 0 is a run time error
Arithmetic expression
Runtime expression
These are 2 errors that can occur during running of java program
Primitive data types include integer , character , void , float etc..which are defined
already inside the language i.e , user can use these data types without defining them
inside the language.
User defined data types are the data types which user have to define while or before
using them.
For example:- In C language , structure and union are used as user-defined data types.
A user-defined data type (UDT) is a data type that derived from an existing data
type. You can use UDTs to extend the built-in types already available and create your
own customized data types.
A composite data type is one which is composed with various primitive data type. A class
defined with various primitive data types such as int, double etc; so it is known as a composite
data type; and it is used to create objects which hold similar types of values and behaviours
(functions).1 Feb 2019
The member access (dot) operator (“.”) is used frequently to access a field or to call a method on an object. object a
= new object(); a. ToString(); The dot operator is also used to form qualified names: names that specify the
namespace or interface (for example) to which they belong.
A wrapper class allows a primitive data type to work in various innovative ways. An integer can use this data type in
various ways. For example, a class 'Hours' will always represent the number wherever it is used. The primitive types
only run with the value, whereas the wrapper class provides a name.
It is used in java for converting a string value to an integer by using the method parseInt()
Float.ParseFloat()
Double.ParseDouble()
Integer.toString()
Double.toString()
Float.toString()
valueOf(String) returns a new Integer() object whereas parseInt(String) returns a primitive int .3 Feb 2009
Package in java can be categorized in two form, built-in package and user-defined package.
import package.name.Class;
It helps you organize your code, avoid naming conflicts, and control access to your classes and interfaces.
Import keyword
Lang package
Import.java.util.*
In this method, the value of each variable in the calling function is In this method, the address of actual variables in the calling function is
copied into corresponding dummy variables of the called function. copied into the dummy variables of the called function.
With this method, the changes made to the dummy variables in the
With this method, using addresses we would have access to the actual
called function have no effect on the values of actual variables in
variables and hence we would be able to manipulate them.
the calling function.
In call-by-values, we cannot alter the values of actual variables In call by reference, we can alter the values of variables through function
through function calls. calls.
This method is preferred when we have to pass some small values This method is preferred when we have to pass a large amount of data to the
that should not change. function.
A runtime error is a program error that occurs while the program is running. Whereas, a syntax error is an error in
the syntax of a sequence of characters or tokens that is intended to be written in a particular programming
language.