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

Why Java is referred to as hot cup of coffee?

The symbol of java used as a hot cup because At the time of taking morning TEA JAMES GOSLIN got

a project which name was green project so thats why he uses the symbol of HOT CUP for JAVA.

What is function overloading in oop?

An overloaded function is a function that has several implementations, the only difference being the number and type of parameters, including usage of the const keyword. Overloads cannot differ by return type alone. The following is a trivial example of function overloading.

const int & max(const int & lhs, const int & rhs){return( lhs>rhs ? lhs : rhs );}

const char & max(const char & lhs, const char & rhs){return( lhs>rhs ? lhs : rhs );}

Since the implementation is exactly the same, regardless of the type of parameters, it would make more sense to enlist the compiler to generate all the possible variants of this overloaded function using a template function. The compiler then generates all the overloads as required, and you only have one function to maintain.

Overloads are better suited to functions that have completely different signatures with a different number of parameters. For instance:

typedef struct rect_tag

{

float width;

float height;

} rect;

const float & Area( const rect & rc ){ return( rc.width * rc.height ); }

const int & Area( const int & width, const int & height ){ return( width * height ); }

const float & Area( const int & width, const float & height ){ return((float) width * height ); } const float & Area( const float & width, const int & height ){ return( width * (float) height ); }

The point of overloading functions is increased flexibility.You don't have to worry about which version of a function you call, nor is there any need to cast parameters to a specific type, since the compiler can work out which version of a function to call simply from the type of parameters you supply. If no suitable overload exists, the compiler will warn you so that you may either provide one, or explicitly cast your variables to a suitable version.

Mixing overloads with default parameter values increases the flexibility further, provided there is no ambiguity regarding which version of the overload is being called.

Sis to jar converter?

SIS files are Symbian and JAR files are java. The software that converts a SIS file to a JAR file is called Sis to Jar Converter 1.7.7.2.

User defined data types in c plus plus?

C++ provides the following fundamental types:

  • A Boolean type (bool)
  • Character types (char and wchar_t)
  • Integer types (int, short, long and long long)
  • Floating-point types (float, double and long double)
  • A type, void, used to signify the absence of information.

Fundamental types correspond to the basic storage units of the machine. From the fundamental types we can construct other types using declarator operators:

  • Pointer types (such as int*)
  • Array types (such as char[])
  • Reference types (such as double& and char&&)

The Boolean, character and integer types are collectively known as the integral types. The integral and floating-point types are collectively known as the arithmetic types.

The fundamental types, pointers, arrays and references are collectively known as the built-in types.

The integral types can be further modified using the signed or unsigned modifiers. Strictly speaking, both long and short are also type modifiers because a short implies a short int. This is simply an artefact from C programming where int was implied in the absence of an explicit type.

Note that aliases (using x = type) and type definitions (typedef) are not types per se, they are simply alternative names for preexisting types. For instance, although wchar_t is a built-in type because it does not require a declaration, in reality it is just an alias for an implementation-defined integral type (typically unsigned short).

From these built-in types we can construct other types:

  • Data structures and classes (such as std::vector and std::string)
  • Enumeration types (enum and enum class)

Data structures, classes and enumeration types are collectively known as user-defined types.

In essence, any type that requires an explicit declaration is a user-defined type. This includes all C++ standard library types such as std::string because we cannot use a std::string object unless we include the header where the type is declared.

When do you declare a variable method and a class final?

When There is No Need to Change the Values of the Variables In Entire lifetime of That variables then we must use that Variable as Final Variable.

What is the main reason behind linked-list?

Linear list for example is array, linked list, stack, ...

It is called linear because all elements in those data structures are in linear sequence. We have first and the last elements in the list. If there is N elements in list, we know that all elements are in such sequence na, na+1.

Non-linear structures would be trees and graphs.

What is node in c language?

No such predefined type, so you can define it as you wish.

How dynamic binding is useful in oops?

  • User-modelling and interface customization. Applications I develop tend to be targeted to very specific audiences. I sometimes provide an initial survey or dialog box that lets the user customize the interface. I've been able to develop a suite of interface components that are easy to customize at run-time by switching their ancestors. If, for example, the user is a Spanish-speaking women who is a computer expert, the application can change the ancestor of on-screen text objects to the Spanish Language ancestor and change the ancestors of other interface objects to the Expert ancestor. If at some time in the presentation the user would like more help, the application can switch ancestors to Normal mode, and then switch back to Expert mode later.
  • Preservation of object properties. Object properties persist across changes in mode, movies, etc. I've found this feature very useful for returning to a stable state after a user-initiated digression.
  • Extensibility. The boss says we need a French language version by 5 pm. No problem. I've already isolated language specific behavior in a Spanish Language ancestor. I copy it, change the Spanish to French, add a line of code to switch the ancestors to the French version and voila!

Of course, all this can be done with globals and nested if-then statements too. I've found this approach easier to maintain and much, much easier to extend and customize.

Write a program to add two numbers using oop?

#include<iostream.h>

#include<conio.h>

void main()

{

int a, b, c;

clrscr();

cout<<"enter the two numbers";

cin>>a;

cin>b;

c=a+b;

cout<<"Addition of two numbers="<<c;

getch();

}

Which are the AWT components in Java?

AWT stands for Abstract window toolkit. AWT gives us the components using which we can create User- Interface based applications in java. Some of the components are:

a. Frame

b. Panel

c. Window

d. CheckBox

e. RadioButton

f. Button

g. TextBox

h. TextArea

i. Etc

C program for polynomial addition?

POLYNOMIAL ADDITION

#include

#include

typedef struct poly

{

int coeff;

int expo;

}p;

p p1[10],p2[10],p3[10];

void main()

{

int t1,t2,t3,k;

int read(p p1[10]);

int add(p p1[10],p p2[10],int t1,int t2,p p3[10]);

void print(p p2[10],int t2);

void printo(p pp[10],int t2);

clrscr();

t1=read(p1);

print(p1,t1);

t2=read(p2);

print(p2,t2);

t3=add(p1,p2,t1,t2,p3);

printo(p3,t3);

getch();

}

int read(p p[10])

{

int t1,i;

printf("\n Enter the total no of terms");

scanf("%d",&t1);

printf("\n Enter the coeff and expo in descending order");

for(i=0;i

scanf("%d%d",&p[i].coeff,&p[i].expo);

return(t1);

}

int add(p p1[10],p p2[10],int t1,int t2,p p3[10])

{

int i,j,k;

int t3;

i=0,j=0,k=0;

while(i

{

if(p1[i].expo==p2[j].expo)

{

p3[k].coeff=p1[i].coeff+p2[j].coeff;

p3[k].expo=p1[i].expo;

i++;j++;k++;

}

else if(p1[1].expo>p2[j].expo)

{

p3[k].coeff=p1[i].coeff;

p3[k].expo=p1[i].expo;

i++;k++;

}

else

{

p3[k].coeff=p2[j].coeff;

p3[k].expo=p2[j].expo;

j++;k++;

}

}

while(i

{

p3[k].coeff=p1[i].coeff;

p3[k].expo=p1[i].expo;

i++;k++;

}

while(j

{

p3[k].coeff=p2[j].coeff;

p3[k].expo=p2[j].expo;

j++;k++;

}

t3=k;

return(t3);

}

void print(p pp[10],int term)

{

int k;

printf("\n\n Given Polynomial:");

for(k=0;k

printf("%dx^%d+",pp[k].coeff,pp[k].expo);

printf("%dx^%d",pp[k].coeff,pp[k].expo);

}

void printo(p pp[10],int term)

{

int k;

printf("\n\n The addition of polynomial:");

for(k=0;k

printf("%dx^%d+",pp[k].coeff,pp[k].expo);

printf("%dx^%d",pp[k].coeff,pp[k].expo);

}

How many types of application in java?

There are three types Java based applications.

1. Stand alone application: Where the application can be run on the machine where the code is available and the application is required on minimum one main method. Developed using J2SE API

Code has to be distributed to get the output

Eg: MSOffice

2. Web Application: Where the application is residing on some container and can be access from any where either from intranet or internet depends on the container exposed and cab developed using J2SE and J2EE

Eg: Any internet based applications like Facebook

Technology: Servlets and JSPs

Framework: Struts and Spring MVC

Architecture: MVC

Requirement: Webserver which has Servlet container and JSP Container

ie WebContainer = Servlet Container + JSP Container

Limitations: Supports only HTTP based requests

3. Enterpise Application: These are superset of Webapplications and with additional functionalities like more sophisticated for Business logic where applications needed more transactions ( DB interations) and security.

Technology: Servlets, JSP and EJBs

Frame Work: Any Frame work including Struts and Spring

Architecture: MVC-n tire

Requirement: EJB Container along with WebContainer

Advantages:

1. Protocol Independent

2. Fully Distributed

3. Full fledged business logic can be implemented with out any other frame works or tools like ORM

4. Supports JTA and JPA

What is the source code for Strassen's matrix multiplication program?

#include
#include
#include
#include "2DArray.h"

#define GRAIN 1024 /* product size below which matmultleaf is used */

void seqMatMult(int m, int n, int p, double** A, double** B, double** C)
{
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
{
C[i][j] = 0.0;
for (int k = 0; k < p; k++)
C[i][j] += A[i][k]*B[k][j];
}
}


void matmultleaf(int mf, int ml, int nf, int nl, int pf, int pl, double **A, double **B, double **C)
/*
subroutine that uses the simple triple loop to multiply
a submatrix from A with a submatrix from B and store the
result in a submatrix of C.
*/
// mf, ml; /* first and last+1 i index */
// nf, nl; /* first and last+1 j index */
// pf, pl; /* first and last+1 k index */
{
for (int i = mf; i < ml; i++)
for (int j = nf; j < nl; j++)
for (int k = pf; k < pl; k++)
C[i][j] += A[i][k]*B[k][j];
}


void copyQtrMatrix(double **X, int m, double **Y, int mf, int nf)
{
for (int i = 0; i < m; i++)
X[i] = &Y[mf+i][nf];
}

void AddMatBlocks(double **T, int m, int n, double **X, double **Y)
{
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
T[i][j] = X[i][j] + Y[i][j];
}

void SubMatBlocks(double **T, int m, int n, double **X, double **Y)
{
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
T[i][j] = X[i][j] - Y[i][j];
}


void strassenMMult(int mf, int ml, int nf, int nl, int pf, int pl, double **A, double **B, double **C)
{
if ((ml-mf)*(nl-nf)*(pl-pf) < GRAIN)
matmultleaf(mf, ml, nf, nl, pf, pl, A, B, C);

else {
int m2 = (ml-mf)/2;
int n2 = (nl-nf)/2;
int p2 = (pl-pf)/2;

double **M1 = Allocate2DArray< double >(m2, n2);
double **M2 = Allocate2DArray< double >(m2, n2);
double **M3 = Allocate2DArray< double >(m2, n2);
double **M4 = Allocate2DArray< double >(m2, n2);
double **M5 = Allocate2DArray< double >(m2, n2);
double **M6 = Allocate2DArray< double >(m2, n2);
double **M7 = Allocate2DArray< double >(m2, n2);

double **A11 = new double*[m2];
double **A12 = new double*[m2];
double **A21 = new double*[m2];
double **A22 = new double*[m2];

double **B11 = new double*[p2];
double **B12 = new double*[p2];
double **B21 = new double*[p2];
double **B22 = new double*[p2];

double **C11 = new double*[m2];
double **C12 = new double*[m2];
double **C21 = new double*[m2];
double **C22 = new double*[m2];

double **tAM1 = Allocate2DArray< double >(m2, p2);
double **tBM1 = Allocate2DArray< double >(p2, n2);
double **tAM2 = Allocate2DArray< double >(m2, p2);
double **tBM3 = Allocate2DArray< double >(p2, n2);
double **tBM4 = Allocate2DArray< double >(p2, n2);
double **tAM5 = Allocate2DArray< double >(m2, p2);
double **tAM6 = Allocate2DArray< double >(m2, p2);
double **tBM6 = Allocate2DArray< double >(p2, n2);
double **tAM7 = Allocate2DArray< double >(m2, p2);
double **tBM7 = Allocate2DArray< double >(p2, n2);

copyQtrMatrix(A11, m2, A, mf, pf);
copyQtrMatrix(A12, m2, A, mf, p2);
copyQtrMatrix(A21, m2, A, m2, pf);
copyQtrMatrix(A22, m2, A, m2, p2);

copyQtrMatrix(B11, p2, B, pf, nf);
copyQtrMatrix(B12, p2, B, pf, n2);
copyQtrMatrix(B21, p2, B, p2, nf);
copyQtrMatrix(B22, p2, B, p2, n2);

copyQtrMatrix(C11, m2, C, mf, nf);
copyQtrMatrix(C12, m2, C, mf, n2);
copyQtrMatrix(C21, m2, C, m2, nf);
copyQtrMatrix(C22, m2, C, m2, n2);

// M1 = (A11 + A22)*(B11 + B22)
AddMatBlocks(tAM1, m2, p2, A11, A22);
AddMatBlocks(tBM1, p2, n2, B11, B22);
strassenMMult(0, m2, 0, n2, 0, p2, tAM1, tBM1, M1);

//M2 = (A21 + A22)*B11
AddMatBlocks(tAM2, m2, p2, A21, A22);
strassenMMult(0, m2, 0, n2, 0, p2, tAM2, B11, M2);

//M3 = A11*(B12 - B22)
SubMatBlocks(tBM3, p2, n2, B12, B22);
strassenMMult(0, m2, 0, n2, 0, p2, A11, tBM3, M3);

//M4 = A22*(B21 - B11)
SubMatBlocks(tBM4, p2, n2, B21, B11);
strassenMMult(0, m2, 0, n2, 0, p2, A22, tBM4, M4);

//M5 = (A11 + A12)*B22
AddMatBlocks(tAM5, m2, p2, A11, A12);
strassenMMult(0, m2, 0, n2, 0, p2, tAM5, B22, M5);

//M6 = (A21 - A11)*(B11 + B12)
SubMatBlocks(tAM6, m2, p2, A21, A11);
AddMatBlocks(tBM6, p2, n2, B11, B12);
strassenMMult(0, m2, 0, n2, 0, p2, tAM6, tBM6, M6);

//M7 = (A12 - A22)*(B21 + B22)
SubMatBlocks(tAM7, m2, p2, A12, A22);
AddMatBlocks(tBM7, p2, n2, B21, B22);
strassenMMult(0, m2, 0, n2, 0, p2, tAM7, tBM7, M7);

for (int i = 0; i < m2; i++)
for (int j = 0; j < n2; j++) {
C11[i][j] = M1[i][j] + M4[i][j] - M5[i][j] + M7[i][j];
C12[i][j] = M3[i][j] + M5[i][j];
C21[i][j] = M2[i][j] + M4[i][j];
C22[i][j] = M1[i][j] - M2[i][j] + M3[i][j] + M6[i][j];
}

Free2DArray< double >(M1);
Free2DArray< double >(M2);
Free2DArray< double >(M3);
Free2DArray< double >(M4);
Free2DArray< double >(M5);
Free2DArray< double >(M6);
Free2DArray< double >(M7);

delete[] A11; delete[] A12; delete[] A21; delete[] A22;
delete[] B11; delete[] B12; delete[] B21; delete[] B22;
delete[] C11; delete[] C12; delete[] C21; delete[] C22;

Free2DArray< double >(tAM1);
Free2DArray< double >(tBM1);
Free2DArray< double >(tAM2);
Free2DArray< double >(tBM3);
Free2DArray< double >(tBM4);
Free2DArray< double >(tAM5);
Free2DArray< double >(tAM6);
Free2DArray< double >(tBM6);
Free2DArray< double >(tAM7);
Free2DArray< double >(tBM7);
}
}

void matmultS(int m, int n, int p, double **A, double **B, double **C)
{
int i,j;
for (i=0; i < m; i++)
for (j=0; j < n; j++)
C[i][j] = 0;
strassenMMult(0, m, 0, n, 0, p, A, B, C);
}


int CheckResults(int m, int n, double **C, double **C1)
{
#define THRESHOLD 0.001
//
// May need to take into consideration the floating point roundoff error
// due to parallel execution
//
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (abs(C[i][j] - C1[i][j]) > THRESHOLD ) {
printf("%f %f\n", C[i][j], C1[i][j]);
return 1;
}
}
}
return 0;
}



int main(int argc, char* argv[])
{
clock_t before, after;

int M = atoi(argv[1]);
int N = atoi(argv[2]);
int P = atoi(argv[3]);

double **A = Allocate2DArray< double >(M, P);
double **B = Allocate2DArray< double >(P, N);
double **C = Allocate2DArray< double >(M, N);
double **C4 = Allocate2DArray< double >(M, N);

int i, j;

for (i = 0; i < M; i++) {
for (j = 0; j < P; j++) {
A[i][j] = 5.0 - ((double)(rand()%100) / 10.0);
}
}

for (i = 0; i < P; i++) {
for (j = 0; j < N; j++) {
B[i][j] = 5.0 - ((double)(rand()%100) / 10.0);
}
}

printf("Execute Standard matmult\n\n");
before = clock();
seqMatMult(M, N, P, A, B, C);
after = clock();
printf("Standard matrix function done in %7.2f secs\n\n\n",(float)(after - before)/ CLOCKS_PER_SEC);

before = clock();
matmultS(M, N, P, A, B, C4);
after = clock();
printf("Strassen matrix function done in %7.2f secs\n\n\n",(float)(after - before)/ CLOCKS_PER_SEC);

if (CheckResults(M, N, C, C4))
printf("Error in matmultS\n\n");
else
printf("OKAY\n\n");

Free2DArray(A);
Free2DArray(B);
Free2DArray(C);
Free2DArray(C4);

return 0;
}

Write a program to accept a character value from the user and sHow is its ASCII value?

You don't need to write a program to do this. Each character (char) is actually an int. If you want to see the ASCII value of a char you can simply:

char c = 'a';

System.out.print( (int)c );

What is a no args constructor?

No args means no arguments. Just like any regular method, a constructor can have zero or more arguments.

No args means no arguments. Just like any regular method, a constructor can have zero or more arguments.

No args means no arguments. Just like any regular method, a constructor can have zero or more arguments.

No args means no arguments. Just like any regular method, a constructor can have zero or more arguments.

Explain different data types in Java with example?

1) List the eight basic data types used in Java. Give examples. The basic types are also known as primitive types and when a variable is declared as any of them, the computer creates what you can call a 'box' in its memory that is big enough to hold a value of that type. Eg int x; // at runtime, computer creates a box called 'x' big enough to hold an integer an integer can be anything between -2147483638 to 2147483647 the other seven primitive (or basic) data types are byte - anything between -128 to 127 short - anything between -32768 to 32767 long - anything between -9223372036854775808 to 9223372036854775807 float - anything between -1.4 * 10^-45 to 3.4 * 10^38 double - anything between -4.9 * 10^-324 to 1.8 * 10^308 char - any single digit on your keyboard (unicode) boolean - true or false source: charatan and kans Java in two semesters pg 19

Difference between HashSet and Linkedhash set in java?

The only difference is that the LinkedHashSet maintains the order of the items added to the Set. It does this by maintaining a doubly linked list containing the hash and the original order of the items. According to Sun, the LinkedHashSet should run nearly as fast as the HashSet.

LinkedHashSet A LinkedHashSet is an ordered version of HashSet that

maintains a doubly-linked List across all elements. Use this class instead of HashSet

when you care about the iteration order. When you iterate through a HashSet the

order is unpredictable, while a LinkedHashSet lets you iterate through the elements

in the order in which they were inserted.

HashSet A HashSet is an unsorted, unordered Set. It uses the hashcode

of the object being inserted, so the more efficient your hashCode() implementation

the better access performance you'll get. Use this class when you want a collection

with no duplicates and you don't care about order when you iterate through it.

What Is a translating program which translates entire source program into machine language program?

High-level source code is converted to native machine code either by an interpreter or a compiler. Interpreted code requires a runtime in order to perform the conversion each time the program is executed whereas compiled code typically produces a standalone machine code program that can be executed without further interpretation. There are some exceptions, most notably Java which is first compiled to Java byte code which is then interpreted by the Java virtual machine on each execution.

An interpreter always converts high-level code to native machine code but, because it must perform the conversion while the code is executing via the interpreter, interpreted code executes much more slowly than native machine code.

Technically, a compiler is simply a program that converts high-level code to a lower-level code. That lower-level code could be another high-level language. For example, a C++ source could be compiled to produce a C source. However, converting the other way around (such as from C to C++) can only be done manually because humans can think in abstract terms far more easily than machines can.

Low-level assembly language source are converted to machine code using an assembler. Machine code can also be disassembled to produce an assembly-like source known as a disassembly, however the resultant code is extremely low-level because there are no named variables (only memory offsets) and all user-comments will have been stripped out during assembly or compilation.

Why can not define private and protected modifiers for variables in interface?

No. Interface variables are supposed to be public static final. Interfaces, like abstract classes, cannot be instantiated, so all variables in an interface must be static final ones. They are public because usually interfaces are used throughout an application, and this will ensure versatility.

What is the function of adapter in java model?

An Adapter is simply a concrete class which implements all the methods of a Listener interface as empty functions. They are convenience classes made because of the tendency to implement Listeners as anonymous classes.

For example, let's say we want to add a Listener to a JFrame to detect a mouse click:

// implementing a Listener

JFrame frame = new JFrame();

frame.addMouseListener(new MouseListener() {

void mouseClicked(MouseEvent e) {

// do something here

}

void mouseEntered(MouseEvent e) {

}

void mouseExited(MouseEvent e) {

}

void mousePressed(MouseEvent e) {

}

void mouseReleased(MouseEvent e) {

}

});

Note how this has a lot of extra code that does nothing.

// implementing an Adapter

JFrame frame = new JFrame();

frame.addMouseListener(new MouseAdapter() {

void mouseClicked(MouseEvent e) {

// do something here

}

});

Note now how we only need to implement one method. This is much cleaner and easier to read.

How do you implement RMI in Java programming?

You download Rmi Updater then load it on and update it and then its done boom!

Check whether the string is palindrome or not without using library functions?

void main()
{ char a[30],b[30];
int i,j,f=1;
printf("\nEnter a string");
gets(a);
for(i=0;a[i]!=\0;i++);
for(j=0;i!=0;i--)
{ b[j++]=a[i];
}
b[j]=\0;
for(i=0;a[i]!=\0;i++)
{ if(a[i]!=b[i])
{ f=0;
break;
}
}
if(f==0)
printf("NOT PALINDROME");
else
printf("PALINDROME");
getch();
}
Syntax error may happen.