0% found this document useful (0 votes)
10 views

Computer Extra Questions

Entry and exit controlled loops refer to whether the test condition is checked before or after executing the loop body, with entry controlled loops like for and while checking first and exit controlled loops like do-while checking last. Examples are given of infinite, nested, and empty loops. Jump statements like break and continue can alter normal flow control by exiting loops prematurely or skipping parts of the loop.

Uploaded by

ayantiksur141014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Computer Extra Questions

Entry and exit controlled loops refer to whether the test condition is checked before or after executing the loop body, with entry controlled loops like for and while checking first and exit controlled loops like do-while checking last. Examples are given of infinite, nested, and empty loops. Jump statements like break and continue can alter normal flow control by exiting loops prematurely or skipping parts of the loop.

Uploaded by

ayantiksur141014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Entry Controlled Loop Exit Controlled Loop

Test condition is checked first, and then


Loop body will be executed first, and then condition is checked.
loop body will be executed.

If Test condition is false, loop body will


If Test condition is false, loop body will be executed once.
not be executed.

for loop and while loop are the


do while loop is the example of Exit controlled loop.
examples of Entry Controlled Loop.

Entry Controlled Loops are used when


Exit Controlled Loop is used when checking of test condition is mandatory after
checking of test condition is mandatory
executing the loop body.
before executing loop body.

What is an infinite loop?

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.

What is meant by nested loop?


A nested loop is a (inner) loop that appears in the loop body of another (outer) loop. The inner or outer loop can be any type: while, do while, or
for. For example, the inner loop can be a while loop while an outer loop can be a for loop. Of course, they can be the same kind of loops too.

What is an empty loop?


An empty loop is a loop which does not have any updation or value of iteration. For example, for(int i = 1;;) (in Java) An empty loop is infinite.
Jump statements are used to alter the normal flow of control within a program. They allow you to make decisions
about which code to execute and when to exit loops prematurely. Java provides three main jump statements: break,
continue, and return.30 Jul 2023

Java break statement


As the name suggests, the break statement is used to break the current flow of the program and transfer the control to the next statement
outside a loop or switch statement. However, it breaks only the inner loop in the case of the nested loop.

The break statement cannot be used independently in the Java program, i.e., it can only be written inside the loop or switch statement.

The break statement example with for loop

Consider the following example in which we have used the break statement with the for loop.

BreakExample.java

1. public class BreakExample {


2.
3. public static void main(String[] args) {
4. // TODO Auto-generated method stub
5. for(int i = 0; i<= 10; i++) {
6. System.out.println(i);
7. if(i==6) {
8. break;
9. }
10. }
11. }
12. }

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.

1. public class ContinueExample {


2.
3. public static void main(String[] args) {
4. // TODO Auto-generated method stub
5.
6. for(int i = 0; i<= 2; i++) {
7.
8. for (int j = i; j<=5; j++) {
9.
10. if(j == 4) {
11. continue;
12. }
13. System.out.println(j);
14. }
15. }
16. }
17.
18. }

Scanner class nextInt() and nextLine()


The package containing the scanner class in java is known as util package

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).

Protected Access Specifier


 Protected will acts as public within the same package and acts as private outside the package.
 Protected will also act as public outside the package only with respect to subclass objects.
 Protected fields or methods cannot be used for classes and Interfaces.
 The Fields, methods, and constructors declared as protected in a superclass can be accessed only by subclasses in other packages.
 The classes in the same package can also access protected fields, methods and constructors as well, even if they are not a subclass of the protected member’s
class.

Example

public class ProtectedTest {


// variables that are protected
protected int age = 30;
protected String name = "Adithya";

/**
* 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());
}
}

Default Access Specifier


 Any member of a class mentioned without any access specifier then it is considered that as Default.
 The Default will act as public within the same package and acts as private outside the package.
 The Default members of any class can be available to anything within the same package and can not be available outside the package under any condition.
 The Default restricts the access only to package level, even after extending the class having default data members we cannot able to access.
Example3

public class DefaultTest {


// variables that have no access modifier
int age = 25;
String name = "Jai";

/**
* 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.

Instance variables Static (class) variables

Instance variables are declared in a Class variables also known


class, but outside a method, as static variables are declared
constructor or any block. with the static keyword in a
class, but outside a method,
constructor or a block.

Instance variables are created when Static variables are created


an object is created with the use of when the program starts and
the keyword 'new' and destroyed destroyed when the program
when the object is destroyed. stops.

Instance variables can be accessed Static variables can be


directly by calling the variable name accessed by calling with the
inside the class. However, within class
static methods (when instance name ClassName.VariableNam
variables are given accessibility), e.
they should be called using the fully
qualified
name. ObjectReference.VariableNa
me.
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.

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 are declared in a Class variables also known


class, but outside a method, as static variables are declared
constructor or any block. with the static keyword in a
class, but outside a method,
constructor or a block.

Instance variables are created when Static variables are created


an object is created with the use of when the program starts and
the keyword 'new' and destroyed destroyed when the program
when the object is destroyed. stops.

Instance variables can be accessed Static variables can be


directly by calling the variable name accessed by calling with the
inside the class. However, within class
static methods (when instance name ClassName.VariableNam
variables are given accessibility), e.
they should be called using the fully
qualified
name. ObjectReference.VariableNa
me.

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

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);
}
}

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

What is the difference between compile and runtime error in Java?


As the name implies, compile time errors occur when the code is built, but the program fails to compile. In contrast, Java runtime errors occur
when a program successfully compiles but fails to execute. If code doesn't compile, the program is entirely unable to execute.

Few library classes of java are


1.google guava
2. java classes
3.HTTP libraries

The wrapper class in Java provides the mechanism to convert primitive into object and object into primitive.
compareTo() equals()

It checks if contents of two strings are same


It compares two strings lexicographically.
or not.

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.

"==" Operator .equals() Method


"==" is an operator in Java and hence it equals() is a method in Java and hence it can be
is can not be overridden. overridden.
It is generally used to compare two
It is used to compare objects. It can not compare
variables of primitive data types but can
primitive data types.
be used to compare objects as well.
It compares the data of the two It compares the memory location of the objects. For
variables, but in the case of objects, it the String objects, it compares them character by
compares the memory locations. character as well if the memory location equality fails.
It takes O(1) time for normal objects and O(n) time for
It takes O(1) time for comparison.
String objects.
"==" Operator .equals() Method
It throws a compile time error if the two It returns a "false" value if the objects are not of the
variables are not of the same data type. same type.

StringBuffer
String

1) The String class is immutable. The StringBuffer class is mutable.

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

Equals and equals ignore case both run on string

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

Try and catch

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.

This keyword this is used to refer the current object

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()

The function of valueOf()is to convert integer to string

valueOf(String) returns a new Integer() object whereas parseInt(String) returns a primitive int .3 Feb 2009

Is() is a function used to check whether a character is a letter or not


If it is a letter then is function return true else false
It converts the character into upper case letters
It converts the characters in the lower case letters
To check whether a character is digit or not
To check whether a character is a blank space or not
It checks whether the value is boolean or not
It checks whether the character is upper case or lower case or not.
A java package is a group of similar types of classes, interfaces and sub-packages.

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.*

Call By Value Call By Reference

While calling a function, instead of passing the values of variables, we pass


While calling a function, we pass the values of variables to it. Such
the address of variables(location of variables) to the function known as
functions are known as “Call By Values”.
“Call By References.

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.

Pointer variables are necessary to define to store the address values of


Values of variables are passed by the Simple technique.
variables.

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.

You might also like