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

What is static inner class?

An inner class declared as static is treated as if it were a normal top-level class.

Write a program to copy the contents of one array into another in the reverse order?

do something like this

char[] myArr = {'a','b'};

StringBuilder sb = new StringBuilder();

sb.reverse();

char[] charArr = sb.toString().toCharArray();

or something like this

char[] charArr = new char[myarr.length];

for (int i=myarr.length-1,j=0;i>=0;i++,j++){

charArr[j] = myarr[i];

}

Why encapsulation does not implement information hiding in object orientation?

Encapsulation also implements data hiding in an object oriented programming design. By encapsulating various methods & data objects into a single class they can also be hidden from all the other classes. You can declare the variables and methods as private and that way you can hide the data from the other classes in the application.

How do you convert an ArrayList to a HashMap?

Unfortunately, there seems to be no built in way of adding all elements of a list to a map. This means that we need to iterate through each element of the ArrayList and add it to the HashMap individually.

/**
* Generic method to add all elements from source into dest.
*/
public static final void addAll(final ArrayList source, final HashMap dest) {

// Iterate through each element in source and put it in dest.
for (int i = 0; i < source.size(); ++i) {

// We will use the index of each element of source as the key in dest.
dest.put(i, source.get(i));

}

}

Sample use:

// Create a new ArrayList and add some stuff to it.
ArrayList list = new ArrayList();
list.add(1); list.add(2); list.add(3);
list.add(4); list.add(5); list.add(6);
list.add(7); list.add(8); list.add(9);

// Print out our list
System.out.println(list);

// Create a new map and call our function
HashMap map = new HashMap();
addAll(list, map);

// Print out to verify identical contents
System.out.println(map);

Sum of all elements in array?

#include

using std::cout;
using std::endl;

int main()
{
int myArrayNumberOfElements(5);
double myArray[myArrayNumberOfElements] = {1.1, 4.5, 5.7, 7.9, 10};
double sum(0);
for (int i(0); i < myArrayNumberOfElements; i++)
{
sum +=myArray[i];

}


cout << endl << "Sum of all elements: " << sum << endl;

system("PAUSE");
return 0;

}

What is the optional in a variable declaration statement?

Optional is the assignment of the value of course.

int number; //Variable Declaration
int number=2; //Assignment Declaration

Can unhandled exceptions be tolerated?

Yes, but they dont always result in acceptable or good behavior.

Exception handling allows developers to detect errors easily without writing special code to test return values. Even better, it lets us keep exception-handling code cleanly separated from the exception-generating code. It also lets us use the same exception-handling code to deal with a range of possible exceptions.

So, it is always a good idea to handle exceptions rather than leave them unhandled

What is type promotion of java?

Type PromotionIn the aforementioned example System.out.println(3 + 4), the expression 3 + 4 is evaluated to 7 and then converted automatically, at runtime, to a String value which is whatprintln expects. This automatic conversion is a somewhat special case of type promotion.

Type promotion is an automatic type conversion from a "lesser" base type to a "greater" one or from any type to String via the toString() method.

A promotion from on base type to another may occur in an arithmetic expression:

Example:double x = 3.14 + 10; /* 10 is promoted to a double */ double y = 12; /* 12 is promoted to a double then assigned */

As mentioned earlier, type promotion occurs from a "lesser" type to a "greater" one where a type is "less" than another if it offers smaller precision. In our previous example, an int offers less precision than a double thus an int value can be promoted to a double as necessary. For the same reason a double cannot be promoted to an int. If an double is used where an int is expected, Java will generate an error:

Example:int z = 2.4; Compile-time error: possible loss of precision

Example program of how to convert infix notation to postfix notation and prefix notation?

/**************************//**********cReDo**********//*****mchinmay@live.com***///C PROGRAM TO CONVERT GIVEN VALID INFIX EXPRESSION INTO POSTFIX EXPRESSION USING STACKS.#include#include#include#define MAX 20char stack[MAX];int top=-1;char pop();void push(char item);int prcd(char symbol){switch(symbol){case '+':case '-':return 2;break;case '*':case '/':return 4;break;case '^':case '$':return 6;break;case '(':case ')':case '#':return 1;break;}}int isoperator(char symbol){switch(symbol){case '+':case '-':case '*':case '/':case '^':case '$':case '(':case ')':return 1;break;default:return 0;}}void convertip(char infix[],char postfix[]){int i,symbol,j=0;stack[++top]='#';for(i=0;i{symbol=infix[i];if(isoperator(symbol)==0){postfix[j]=symbol;j++;}else{if(symbol=='(')push(symbol);else if(symbol==')'){while(stack[top]!='('){postfix[j]=pop();j++;}pop();//pop out (.}else{if(prcd(symbol)>prcd(stack[top]))push(symbol);else{while(prcd(symbol)<=prcd(stack[top])){postfix[j]=pop();j++;}push(symbol);}//end of else.}//end of else.}//end of else.}//end of for.while(stack[top]!='#'){postfix[j]=pop();j++;}postfix[j]='\0';//null terminate string.}void main(){char infix[20],postfix[20];clrscr();printf("Enter the valid infix string:\n");gets(infix);convertip(infix,postfix);printf("The corresponding postfix string is:\n");puts(postfix);getch();}void push(char item){top++;stack[top]=item;}char pop(){char a;a=stack[top];top--;return a;}//mail me: mchinmay@live.com

What happens if a constructor is not defined in class?

Nothing happens. The compiler successfully compiles the class. When a class does not have a specific constructor, the compiler places a default no argument construtor in the class and allows you to compile and execute the class.

public class Test {

}

and

public class Test {

public Test(){

}

}

are one and the same.

What is meaning for BufferedReader br equals new BufferedReader new InputStreamReade System in?

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

This line will create a new BufferedReader object, which reads from System.in (standard input). The InputStreamReader part is used to convert System.in (which is an InputStream) to a Reader object (which can be used by BufferedReader).

After creating br, you can read input from the user:

String input = br.readLine();

The line above will allow the user to type in anything they want, press the button, and have what they typed in stored in input.

Does C language has a interpreter?

There might be C language interpreters, but they have no or minimal importance in actual programming.

What are disadvantages of file system?

File system data management (or flat-file databases) served as the only method of file storage and retrieval before the advent of database management systems (such as relational databases). While retaining some use, flat-file databases suffer from poor accessibility, data redundancy, lack of standard file access and the inability to organize data.

Wap to display string in the ascending order in java?

public class CreateDatabase{ public static void main(){ int a=10; int b=20; int result =a+b; System.out.println(result); } }

How do you switch off Java updates for Vista?

First, open up the Control Panel: Start -> Control Panel If you're using the default view for Control Panel, click on "Programs." Now click on "Java" (If you use the classic view option, this will show up right away and you can double click it.) When the Java Control Panel pops up, click on the Update tab, and uncheck the "Check for Updates Automatically" box. There may be a warning message that pops up; click on the Never Check button to turn off updates. Now click on the OK button and close the Control Panel window and you're all set.

What is the methods with the parameters of an AWT?

AWT (Abstract Window Toolkit) is a top-level Java package. Listing out the hundreds or thousands of methods would be a waste of effort.

See the related link below for the Java documentation on the AWT package.

Difference between reference and object in java?

On the lower level of Java, a "reference" can be thought of like a pointer in C. It is essentially an integer which refers to (points to) a location in memory where the object data exists.

// "button" is a reference to a JButton with a "1" on it (the object).

JButton button = new JButton("1");

C program to find the mean median mode?

try this---> http://c-pgms.blogspot.com/2008/08/program-to-find-meanmedianand-mode_22.html

How do you represent or in java?

A boolean or comparison in Java is made with the operator.

boolean a = true;

boolean b = false;

if( a b) {

...

}

A bitwise or comparison in Java is made with the | operator.

int n = 1;

int m = 2;

if( n | m == 3 ) {

...

}

What happens if the controlling expression in a do while loop is initially false?

The do-while loop is designed specifically for such situations, where you want the loop to execute once irrespective of the loop expression.

The loop would execute once and then terminate because, the loop controlling expression is false.

If you note the syntax properly

do {

...

...

...

} while(condition)

The condition is executed only after one iteration of the loop and hence the code would execute once irrespective of the loop expression result.

What is packaged food?

This is packaging for food. A packaging that provides protection, resistance from temperature, and special physical, chemical needs. It must handle and represent all the nutrition facts, label and all other related information about food.

How do you add tangent in calculator in java?

You are able to calculate the tangent of a number using the Math class (java.lang.Math). The tan(double a) method is the one you are looking for. For example: double a = 2.5; double tangent = java.lang.Math.tan(a);