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

Dynamic binding makes it possible for objects to behave polymorphically even when we do not know the runtime type of the object.

Consider the following:

struct A {

int x;

int data() const { return x; }

};

struct B {

int y; int data() const { return y; }

};

Given that objects of type A and B are not of the same type, how can we pass just one instance of either type to the same function? One way would be to pass two separate references:

void foo (A& a, B& b) {

// ...

}

The problem with this is that we must have two objects to refer to whenever we call this function. If we only have one then we must instantiate a dummy argument for the other. That's hardly intuitive. And how would we differentiate the dummy from the actual argument? We could use pointers instead of references, passing nullptr for the unused argument, but that only serves to make it even less intuitive; a caller might easily pass nullptr for both arguments!

We could use function overloading and create two separate functions:

void foo (A& a) {

// ...

}

void foo (B& b) {

// ...

}

That's certainly more intuitive from the caller's point of view, but it's now a nightmare for the person who has to maintain this code in future. Every time we make a new class for this function we must define a completely new overload to handle it. The ideal here is to have just one function with one implementation.

We could use a void pointer.

void foo (void* p) {

// ...

}

That's certainly an improvement, but we still risk passing a nullptr. If we want to guard against this we must pass a reference, but we cannot declare a reference of unknown type. And the maintenance nightmare still exists because we still have to determine the actual runtime type before we can dereference the pointer.

Let's look at those type definitions again.

struct A {

int x;

int data() const { return x; }

};

struct B {

int y;

int data() const { return y; }

};

Although they are completely different types, they both have a common method, data(). We can exploit that fact by percolating this "pure" interface into a common base class:

struct Base {

virtual int data() const = 0;

virtual ~Base() {}

};

struct A : Base {

int x;

int data() const override { return x; }

};

struct B : Base {

int y;

int data() const override { return y; }

};

Now we can define our function in terms of this one common, abstract base class:

void foo (Base& base) {

int z = base.data();

// ...

}

Problem solved! Now we can derive as many different classes as we want from Base and the foo() function will work as expected without any changes. Note that the function makes no reference whatsoever to the actual runtime type. This is because it doesn't need to know the runtime type, it only needs to know the interface defined in the one and only class that it does need to know about, the Base class!

Note that since the Base class has no data, there is no memory overhead:

assert (sizeof(A)==sizeof(B)==sizeof(int));

The only real overhead is in the virtual tables which is where dynamic binding comes in. When we invoke the Base::data() virtual method, that method is dynamically bound to either A::data() or B::data(), depending on which specific class we passed to the foo() function.

Note also that when we declare a virtual method, we must also declare the destructor virtual as well. This ensures that the most-derived class is always destroyed first, even if we only hold a pointer to its base class:

Base* p = new A {42};

// ...

delete p; // OK -- A is destroyed before Base due to virtual destructor.

Again, virtual destruction is an example of dynamic binding.

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

How do you make an array of 56?

In Java:

int[] myArray;
// or: int myArray[]

followed by:

myArray = new int[16];

Instead of int, you can use any other data type, including a class.

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.

What is the difference between main function in C java?

// C

int main(int argc, char** argv) {

return 0;

}

// Java

class MainClass {

public static void main(String[] args) {

}

}

Differences:

* Java main method must be in a class; C has no class, so no such requirement exists. * Both accept an array of command-line args. * ** Java arrays know how long they are; C does not and thus the main function must have an additional parameter to know the length of the array. * The Java main method has a specific format; the C main function seems to be allowed to differ. * ** The C main function seems to be able to accept different numbers of arguments and have different return types (assuming the implementation allows it).

Why can't we use a keyword as a variable name?

It is because, the keywords are defined in the java language for a specific purpose. If we start declaring variables with the same name, the compiler would not know if you are trying to use a variable are trying to tell it to perform an action and hence keywords cannot be used as a variable name.

What is volatile modifier?

Volatile variables are variables that may change value while they are being operated upon. For instance, a hardware clock is constantly updated by the operating system, thus its value is constantly changing. If you refer to the value, you must declare it volatile because the value is outwith the control of the program.

How do you call constructor?

Constructors in java cannot be invoked explicitly. They are invoked automatically when an object is created. In java if u dont write any code for constructor, by default compiler inserts a zero argument constructor. If required, it can be overrided.