answersLogoWhite

0

📱

Java Programming

The Java programming language was released in 1995 as a core component of the Java platform of Sun Microsystems. It is a general-purpose, class-based, object-oriented language that is widely used in application software and web applications.

5,203 Questions

How the new operator works in java?

1. It is the only way to create object. 2. New is a keyword. 3. New operator allocates memory for an object. 4. It is a bit faster and clever way of creating objects or instances.

What is meant by the complexity of an algorithm?

Complexity of an algorithm is the study of how long a program will take to run, depending on the size of its input & long of loops made inside the code

Specifically, the complexity of an algorithm is a measure of how long it takes to complete (give an answer) relative to increasing sizes of input data. Thus, complexity is not concerned with how long it took the algorithm to run using X amount of data. Rather, it is concerned with the relationship in runtime when using X amount of data, 2X amounts of data, 10X amounts of data, etc. While complexity usually refers to execution time, it can also be applied to other resource usage (for example, memory allocation). In all cases, complexity is concerned with the relationship between the rate of increase in resource consumption and the rate of increase of the size of the data set being worked on.

Complexity is closely related to the concepts of scalingand efficiency, but is NOT an exact equivalence to either.

Why it is necessary to avoid infinite loop in program design?

It is not necessary to avoid infinite loops. You are perhaps confusing infinite loops with endless loops which are to be avoided at all costs. An endless loop is an infinite loop that has no reachable exit condition; the loop will iterate until we forcibly terminate the program.

We use the the term infinite loop in the sense that it is impossible to measure or calculate when the exit point will be hit. the following are all examples of infinite loops in their simplest form:

for (;;) {

// ...

}

while (true) { // ... }

do while (true) {

// ...

}

endless:

// ...

goto endless;

The conditional expressions in each of these loops can never be false thus we cannot easily determine when these loops will exit. We typically use infinite loops when there are many exit conditions to consider and it is either impractical or inefficient to evaluate all of those conditions via the controlling expression alone. We take it as read the exit conditions are contained within the body of the loop.

If the body of the loop has no reachable exit condition then it becomes an endless loop. It is the programmer's responsibility to ensure that all infinite loops can exit at some point.

What type is data?

There Are Two main types of data. Qualitative data are expressed As numbers, obtained by counting or measuring. Another type of data is called an inference.An inference is a logical interpretation based on prior knowledge or experience.

What are return statements in java?

you can understand what is the purpose of return in java after this simple example

Basically return use in java to return some values from a method

note: this sample will work on all java versions, i test it on JDK 1.4

enjoy the code.

file name : Class1.java

-----

package mypackage1; //package name

public class Class1 //class name + file name the

{

public String add(int a,int b) //a method in the class; its type is String and it take a and b variables; a and b are int

{

int answer = a+b; //this is some code in the class; maybe some operations; no need to know whats happening in this method

System.out.println("I'm in Class1"); //this is some code in the class; maybe some operations; no need to know whats happening in this method

return " I'm the returned value "+ answer; ////this is what you need to take from the method

}

public static void main(String [] arg)

{

Class1 c1 = new Class1(); //creating c1 object from Class1 Class

System.out.println(c1.add(10,6)); //printing the returned values from the method;

}

}

-----------------

output:

I'am in Class1

I'am the returned value 16

Return the number of even ints in the given array java?

int getNumEvens(final int[] nums) {

int numEvens = 0;

for(int i = 0; i < nums.length; ++i) {

if(nums % 2 == 0) {

++numEvens;

}

}

return numEvens;

}

How many base class and derived class can be created in inheritance?

As many as required. The only practical limits are those imposed by the hardware. For instance, 32-bit systems can only address a maximum of 4GB, but only 2GB is actually available to applications. On 64-bit systems there is no practical limit other than hard-drive space (for the virtual memory paging file).

Remember that every class of object requires memory to store its member variables (plus padding for alignment), which has to be multiplied up by the number of instances of those classes. Thus the more complex the hierarchy, the fewer instances you can create overall. Since alignment padding can add a substantial overhead, it's best to declare member variables from largest to smallest within each class because memory is allocated in the same order the members are declared. If a v-table is required (which it typically will be in a multi-level hierarchy) this will consume additional memory: essentially one function pointer per virtual function per override.

That said, it is difficult to imagine any hierarchy so large that you will hit a memory limitation, even on a 32-bit system, unless you happen to embed a particularly large member variable in your class, such as a hi-resolution image or video, rather than use a disk-based file stream. Aside from that the main concern is in how many instances of that hierarchy can you physically construct at one time. However, you need only look at some of the hierarchies within the Microsoft Foundation Classes to realise that your probably just scratching the surface of what is actually possible.

What type of operator is used to compare two values?

Conditional operators are used to compare two values. The result of a comparison is either true or false. Boolean data types can hold the values true or false.

Here's a list of operators.

= Equal to

> Greater than

< Less than

>= Grater than or equal to

<= Less than or equal to

<> Not equal to

Can abstract method be overriden?

An overridden method is one which has already been implemented in the parent class with the same signature but has again been coded in the current class for functionality requirements. An abstract method is one which has only been declared in a class and would have to be implemented by the class that extends this abstract class. The implementation for the method is not available in the class in which it is declared.

In java using command line argument write prime number program?

/* Program to print prime numbers up to given number */

import java.util.*;

public class Prime

{

public static void main(String args[])

{

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number : (it prints prime numbers upto your given number)");

int n=sc.nextInt();

System.out.println("The Prime number upto "+n+" are : ");

while (n>=0)

{

int count=0;

for (int i=1;i<=n ;i++)

{

if (n%i==0)

{

count++;

}

}

if (count==2)

{

System.out.print(n+" ");

}

n--;

}

}

}

What are the advantages of class?

tuition classes are give extra knowledge

What is the difference between traditional programming and object oriented programming?

Object oriented programming is a design paradigm supported by the languages compiler where aspects of the program are thought of as objects. The code (i.e. functions) and variables required by those methods are grouped together into objects. The object exposes methods to the programmer so the object can be manipulated. There are more advanced aspects of object oriented programming such as polymorphism and inheritance that allow more high level and abstract usage of objects.

Function oriented programming treats functions and variables separately. Functions are used like machines; they take in variables, manipulate, and return them. The variables and functions required to accomplish a task aren't grouped together as in object oriented programming.



For a large project object orientated programming is more flexible and easier to understand. Small, very specific tasks, can some times be best accomplished with straight forward function oriented programming.

Full java program to find the given string is palindrome or not?

Answer 1:private static final boolean isPalindrome(final String str) {

return str.equals((new StringBuilder(str)).reverse().toString());

}

Answer 2:import java.io.*;

class StringPalindrome

{

protected static void main()throws IOException

{

BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the String: ");

String s=in.readLine(),n="";

for(short i=(short)(s.length()-1);i>=0;i--)

{

char ch=s.charAt(i);

n+=ch;

}

if(s.equalsIgnoreCase(n))

System.out.print("Palindrome!!!!");

else

System.out.print("Not Palindrome!!!!");

}}

Write a program to check wheather a vowel or not by using if else construct in javac?

import java.util.Scanner;

class Vowels{

public static void main(String[]args){

int c=0;

Scanner obj=new Scanner(System.in);

System.out.println("Enter your String:");

String b=obj.nextLine();

for (int i=0;i<b.length();i++) {

if (b.charAt(i)==('a')b.charAt(i)==('e')b.charAt(i)==('u')b.charAt(i)==('i')b.charAt(i)==('o')b.charAt(i)==('A')b.charAt(i)==('E')b.charAt(i)==('U')b.charAt(i)==('I')b.charAt(i)==('O')){

c++;

}

}

System.out.println("Number of vowels found: "+c);

}

}

Is java is front end or back end?

Backend = software running on a server connecting to databases, mainframes, or simply just doing stuff.

Frontend = the part a user interacts with. It might be an applet, web page, Swing application, or just about anything that can communicate with the backend.

Why Can't I download Java software?

There are many ways to download Java. You could google 'java download' or type in any variation with the words java and download in a search engine of your choice. Also, you could ask a friend to download it for you. For instance, if you have a friend named Mike that is not completely computer illiterate, you could say, "Hey Mike, will you download Java for me, please?". But, the quickest way to download Java is to go to:

http://www.java.com/en/download/

Which data type in Java is the widest?

Data types and Variables

Data types are used to define what kind of information is going to assign to a variable. Java contains two kinds of built-in data types are object oriented and non-object oriented data types. The object oriented data types are defined by a classes. The non object oriented data types are called primitive data types. Java defines the range and the behavior for each data type. However unlike other languages in java three is no need to specify a size of the data type because of it's portability.

Primitive Data types

There are eight primitive data types available in Java are segregated in terms of Integers, Floating-Point and Boolean data types.

Integers

There are four data types to store Integers. They are byte, short, int and long These data types are signed which means that they can hold either positive or negative values.

The range and size of each data type has been given belowTypeSizeRangebyte8-128 to 127short16-32,768 to 32,767int32-2,147,483.648 to 2,147,483,647long64-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Most commonly used integer type is int. The smallest integer type is byte. It is used to store the raw binary data. The short type is mostly applicable to 16 bit computers.

/* This is a example for using Integer Data types

Find the addition of two numbers */

class addition

{

public static void main(String args[])

{

int i=8;

int j=7;

int k;

k= i + j ;

System.out.println(" The addition of " + i + "and" + j + " is" + k);

}

}

The output will be

The addition of 8 and 7 is 15

Here in this example the data type int has assigned to the variables i ,j and k. We can also declare the variables with the same type of data types in the same statement separated by comma. When we need an integer that has a range greater than int, then we should go for long.

Floating-Point Types

Floating point numbers are numbers with decimal point. There are two kinds of floating-point types, floatand double. The range and size of float and double has given here.TypeSizeRangefloat321.4e -045 to 3.4e+038double644.9e-324 to 1.8e+308

Among these two, double is the most commonly used because all of the math function in Java's library use double values.

/* This is example for using floating-point Data Types

Find the square root of given numbers */

class squareroot

{

public static void main(String args[])

{

double x,y,z;

x=13;

y=17;

z=Math.sqrt(x+y);

System.out.println(" The Square Root is " +z);

}

}

The output will be

The Square Root is 5.477225575051661

In this example we have used the floating point data type double. Here we have used the new termMath.sqrt(), Math is a Java's built in class and sqrt() is a method in class Math.

Characters

The char type is used for individual characters, such as letters, numbers, punctuation and other symbols. In Java characters are not 8 bit type like they are in other computer languages. Java uses Unicode to represent the characters. Unicode is a character set that can represent all of the characters found in all human language. So, in Java char type is unsigned 16 bit type range from 0 to 65,536. Since the standard 8 bit ASCII set is subset of Unicode, the ASCII characters are still valid Java characters.

A character variable can be assigned a value by enclosing the character in single quotes.

For example, the letter x assigns to variable ch as like this

char ch;

ch='x';

Since char is an unsigned 16-bit type, it is possible to perform the arithmetic manipulations on a char variable.

/* This is a example for using the Data type character

Doing some arithmetic manipulation in character */

class CharManipulation

{

public static void main(String args[])

{

char ch='x';

System.out.println(" Now the ch is " +ch);

ch++;

System.out.println(" Now the ch is " +ch);

ch++;

System.out.println(" Now the ch is " +ch);

}

}

The Output will be

Now the ch is x

Now the ch is y

Now the ch is

In this example the variable ch is declared char variable and it has assigned to the value of 'x'

In the next line we are incrementing the value of ch by 1. So the corresponding Unicode value will be assigned to ch now. So the letter 'y' will be assigned. In the next line also we are doing the same procedure of incrementing the ch value by 1. So now the value of ch will be 'z'.

Boolean Type

The Boolean type holds either true or false using the reserved word true or false. The expression of the variable using boolean datatype will be either true or false.

For example the boolean data type will be assigned as

boolean b;

b=true;

or

boolean b= true;

Variable declaration in Java

We can define Variable is a place where information can be stored while program is executing. The value of the variable can be changed at any point in the program. The syntax of the variable declaration is

type var-name;

where type is the data type of the variable, and var-name is the name of the variable. We can assign any valid data type to a variable. As we had seen before the data type will decide what kind of values can be assigned to a variable. Variables can be declared anywhere in the Java program, only thing is it should be declared before they can be used. As we said earlier when we want to declare several variables with the same data type, we can declare all of them in the same statement. Variable names in Java must start with a letter, a underscore character( _ ) or with a dollar sign. They cannot start with a number.

There are three kinds of variables can be declared in Java called as instance variable, class variable and local variable. Here we are going to discuss about the local variables. The local variables can be assigned inside the method or inside the block of statements within a method. All the local variables must be initialized before it is been used.

For Example

class VarInit

{

public static void main(String args[])

{

int i=12,j=16,k=30;

System.out.println(" The value of i is " + i );

System.out.println(" The value of j is " + j );

System.out.println(" The value of k is " + k );

}

}

The Output will be

The value of i is 12

The value of i is 16

The value of i is 30

Here in this example since the variables i,j and k hold the same data type we declared them in the same statement. Notice that they have also been initialized with some values.

Why user defined applet class declared public?

Well, it was probably an aesthetic decision on the part of the Java developers, and consider this: The server needs to access some of the applet's attributes, so doesn't it make sense that the applet is public? If the applet was private, nobody would be able to access it.

Where to use overloading?

Default arguments are often considered to be optional arguments, however a default argument is only optional in the sense that the caller need not provide a value for it. The function must still instantiate the argument and must assign the appropriate value to it so, insofar as the function is concerned, the argument is not optional.

To implement a function with a truly optional argument, we can define two overloads of that function, one that accepts the optional argument (without specifying a default value) and one that does not accept the argument. In this way we can define two different implementations, one that uses the argument and one that does not.

void f (); // implementation that does not use the argument

void f (int); // implementation that does use the argument

In many cases, a default argument incurs no significant overhead over that of overloading. Thus we'd only use overloading to implement an optional argument where there is a significant overhead incurred by a default argument. Even so, we must also be aware that by eliminating the overhead within the function itself we may simply be passing that overhead back to the callers, because some or all of them would then have to decide which overload to call, resulting in code duplication that would likely be best handled by the function itself.

54321 4321 321 21 1 what is the code in for loop?

public static void main(String[] args){

for(int i=0;i<5;i++)

{

for (int j=

What type of list sequences items?

a Bulleted list

No, that's incorrect, I'm afraid.

A bulleted list is used when no sequencing is necessary.

A numbered or lettered list is used to indicate a particular sequence of items.

How many dimensions you can declare in array?

It seems that the number of allowed array dimensions is implementation specific and not set by the Java specifications. I'm sure that any Java implementation will allow a reasonable number of dimensions for any project you have.

After a quick test, it seems that Java is not limited by an arbitrary number so much as a practical value. If you add hundreds of array dimensions, Java will allow you to do so as long as you have enough memory allocated for Java. After a bit of copy-pasting the program no longer ran, exiting with a StackOverflowError.

What is the use of main method in java?

The Main method is the method in which execution to any java program begins.

A main method declaration looks as follows:

public static void main(String args[]){

}

The method is public because it be accessible to the JVM to begin execution of the program.

It is Static because it be available for execution without an object instance. you may know that you need an object instance to invoke any method. So you cannot begin execution of a class without its object if the main method was not static.

It returns only a void because, once the main method execution is over, the program terminates. So there can be no data that can be returned by the Main method

The last parameter is String args[]. This is used to signify that the user may opt to enter parameters to the java program at command line. We can use both String[] args or String args[]. The Java compiler would accept both forms.