1. What are scripting languages? Give examples.
SCRIPTING LANGUAGE:A high-level programming language that is interpreted by another program at runtime rather than compiled by the computer's processor as other programming languages is called scripting language.Scripting languages which can be embedded within HTML commonly are used to add functionality to a Web page.
JavaScript, ASP, JSP, PHP, Perl, Tcl and Python are examples of scripting languages.
Advantages:
•easy to learn and use
•allow complex tasks to be performed in relatively few steps
•editing and running code is fast.
Disadvantage:
• executable code can inadvertently be downloaded from a remote server to a web browser's machine, installed and run using the local browser's interpreter without valid authenticity.
2. What are copy and reference semantics? How are they available in C++ and Java?
3. What are storable values? List some storable values in Java.
4. What are expression-oriented languages?
EXPRESSION ORIENTED LANGUAGE:An expression-oriented language is an imperative languge in which distinctions between expressions and commands are eliminated.Evaluating any expression yields a value and may also have side effects.
Algol-68, Scheme, ML and C are examples of expression oriented languages.
The assignment "V = E" can be defined to yield the value of E together with side effect of storing that value in variable yielded by V. The benefit of this design is to avoid duplication between expressions and commands.Thus it achieves certain simplicity and uniformity.Expression oriented languages positively encourage use of side effects leading to cryptic programming style.
5. Why is a language that supports only static storage allocation limited?
6. What are the features of ALGOL family of languages?
The main features of ALGOL family are:
• functions and subroutines
• value and reference-based parameters
• procedural and imperative emphasis
• code blocks (StructuredProgramming) which include these statements at least:
• if/else
• while
• switch or case
Most modern programming languages - C, C++, VB, Delphi, Java, Python, and even Perl use syntax that originated with the Algol Language. So they are called Algol Family languages.
7. Why does a C/C++ programmer enclose a union inside another structure?
Unions are often nested within a structure that includes a field giving the type of data contained in the union at any particular time. This is an example of a declaration for such a union:
struct x
{
int type_tag;
union
{
int x;
float y;
}
}
The most common way of remembering what is in a union is to embed it in a structure with another member of the structure used to indicate the type of thing currently in the union.
It is because of unions that structures cannot be compared for equality. The possibility that a structure might contain a union makes it hard to compare such structures; the compiler can't tell what the union currently contains and so wouldn't know how to compare the structures.
Also structure inside union is used when we need to access the variable of abstract type and also its lower part .
ex: Individual bit of integer and whole integer can be accessed with this.
8. Why are recursive types not available in most languages? What concepts are used to define recursive
types?
9. In many programming languages, strings are defined as arrays of characters. Compare the
consequences when the array is static, dynamic and flexible. (Assignment,Comparison,Concatenation)
10. What are visibility and lifetime? What is the purpose of local, global and static variables? What are initialized values in C/C++ and Java?
11. Write the corresponding if else statements:
int a, b,c, d, e;
a?b:c;
a?b:c?d:e;
a?b?c:d:e;
a?b:c?d:e?;
a?b:c
if(a)
{
b;
}
else
{
c;
}
a?b:c?d:e
if(a)
{
b;
}
else
{
if(c)
{
d;
}
else
{
e;
}
}
a?b?c:d:e
if(a)
{
if(b)
{
c;
}
else
{
d;
}
}
else
{
e;
}
a?b:c?d:e
if(a)
{
if(b)
{
c;
}
else
{
if(c)
{
d;
}
else
{
if(e)
{
}
}
}
}
13. How much total space is required for the following C++ code fragment?
{
int a,b,c;
{
int d,e;
{
int f;
}
}
int g,h,i;
}
Assume each integer occupies 4 bytes.
14. State whether the following statements are True or False:
(i) C++ programs use dynamic scoping when accessing a macro
(ii) In C++ expressions, operands in binary evaluation are evaluated in a left-to right order.
15. Why do Java compilers generate byte code instead of machine code?
16. What does the following C code do?
int i=2, j=4;
int list[]={6,7,18,-8,22,104};
list[++i,j]=77;
17. What is the output of the following Java program?
System.out.println(1.0 + 1.0 + 1.0e17 - 1.0e17);
System.out.println(1.0e17 - 1.0e17 + 1.0 +1.0);
The output for the first statement is zero.
Reason: As ‘+’ and ‘–‘ have same precedence the expression will be evaluated from left to right. Initially 1.0+1.0 is evaluated and the result 2.0 is added to 1.0e17. But as 1.0e17 is very large compared to 2.0 the result will be approximated to 1.0e17 from which 1.0e17 is subtracted yielding the result zero.
1.0+1.0 =2.0
2.0+1.0e17 = 1.0e17
1.0e17-1.0e17 = 0
The output for the second statement is 2.0.
Reason: As ‘+’ and ‘–‘ have same precedence the expression will be evaluated from left to right. 1.0e17 is subtracted from 1.0e17 and the result zero is added to 1.0. Then 1.0 is added to 1.0 which gives the answer 2.0.
1.0e17-1.0e17 = 0
0+1.0 = 1.0
1.0+1.0 = 2.0
19. What are the differences between Java and C++ arrays?
In Java, arrays are first-class objects, while in C++ they are merely a continuous run of their base objects, often referred to using a pointer to their first element and an optional length. In Java, array objects are bounds-checked and contain information about their own length, while in C++ any subsequence can be treated as an array in its own right. Both C++ and Java provide container classes (std::vector and java.util.ArrayList respectively) which are resizable and store their size.In both languages, arrays have a fixed size.
Unlike C++, Java provides is a length member, which tells you how big the array is. An exception is thrown if you attempt to access an array out of bounds. All arrays are instantiated in dynamic memory and assignment of one array to another is allowed. However, when you make such an assignment, you simply have two references to the same array. Changing the value of an element in the array using one of the references changes the value insofar as both references are concerned.
declaration of an array of java:
datatype array_name[ ] = new data type[size];
declaration of an array in c++:
datatype array_name[]={1,2,3};
20. Explain the difference between type declaration and type definition in C++ with an example.
21. What does the following Java statement do? (where marks is of type int)
boolean is A = (90<=marks<=100);
22. What is the output of the following Java program?
double x = double (3/5);
System.out.println(x);
23. What happens when you evaluate the following expressions in Java?
17/0
17%0
17.0/0.0
17.0%0.0
24. What happens when overflow or underflow occurs in integers in Java and C?
25. Consider the following expression
e1 ? e2 : e3
If e2 and e3 are of different types then the result would be of which type?
Ternary Operator (C/C++):
A ternary operator has the following form
exp1 ? exp2 : exp3
The expression exp1 will be evaluated always. Execution of exp2 and exp3 depends on the outcome of exp1. If the outcome of exp1 is non zero exp2 will be evaluated, otherwise exp3 will be evaluated.
Return Type:
The ternary operator has return type. The return type depends on exp2, and convertibility of exp3 into exp2 as per usual\overloaded conversion rules. If they are not convertible, the compiler throws an error.
For example:
The following program compiles without any error. The return type of ternary expression is expected to be float (as that of exp2) and exp3 (i.e. literal zero – int type) is implicitly convertible to float.
#include
using namespace std;
int main()
{
int test = 0;
float fvalue = 3.111f;
cout << (test ? fvalue : 0) << endl;
return 0;
}
The following program will not compile, because the compiler is unable to find return type of ternary expression or implicit conversion is unavailable between exp2 (char array) and exp3 (int).
#include
using namespace std;
int main()
{
int test = 0;
cout << test ? "A String" : 0 << endl;
return 0;
}
We can observe that exp2 is considered as output type and exp3 will be converted into exp2 at runtime. If the conversion is implicit the compiler inserts stubs for conversion. If the conversion is explicit the compiler throws an error. If any compiler misses to catch such error, the program may fail at runtime.
26. Let C union construct consists of two fields long and float. Can one value be converted into other?
Please post answers to these questions as comments for this post. They will be later integrated in it.
SCRIPTING LANGUAGE:A high-level programming language that is interpreted by another program at runtime rather than compiled by the computer's processor as other programming languages is called scripting language.Scripting languages which can be embedded within HTML commonly are used to add functionality to a Web page.
ReplyDeleteJavaScript, ASP, JSP, PHP, Perl, Tcl andPython are examples of scripting languages.
Advantages:
•easy to learn and use
•allow complex tasks to be performed in relatively few steps
•editing and running code is fast.
Disadvantage:
• executable code can inadvertently be downloaded from a remote server to a web browser's machine, installed and run using the local browser's interpreter without valid authenticity.
EXPRESSION ORIENTED LANGUAGE:An expression-oriented language is an imperative languge in which distinctions between expressions and commands are eliminated.Evaluating any expression yields a value and may also have side effects. Algol-68, Scheme, ML and C are examples of expression oriented languages. The assignment "V = E" can be defined to yield the value of E together with side effect of storing that value in variable yielded by V. The benefit of this design is to avoid duplication between expressions and commands.Thus it achieves certain simplicity and uniformity.Expression oriented languages positively encourage use of side effects leading to cryptic programming style.
ReplyDeleteThe main features of algol family are:
ReplyDelete• functions and subroutines
• value and reference-based parameters
• procedural and imperative emphasis
• code blocks (StructuredProgramming) which include these statements at least:
• if/else
• while
• switch or case
Most modern programming languages - C, C++, VB, Delphi, Java, Python, and even Perl use syntax that originated with the Algol Language. So they are called AlgolFamily languages.
a?b:c
ReplyDeleteif(a)
{
b;
}
else
{
c;
}
a?b:c?d:e
if(a)
{
b;
}
else
{
if(c)
{
d;
}
else
{
e;
}
}
a?b?c:d:e
if(a)
{
if(b)
{
c;
}
else
{
d;
}
}
else
{
e;
}
a?b:c?d:e
if(a)
{
if(b)
{
c;
}
else
{
if(c)
{
d;
}
else
{
if(e)
{
}
}
}
}
the 4th part is wrong in the above solution.
Deletea?b:c
ReplyDeleteif(a)
{
b;
}
else
{
c;
}
a?b:c?d:e
if(a)
{
b;
}
else
{
if(c)
{
d;
}
else
{
e;
}
}
a?b?c:d:e
if(a)
{
if(b)
{
c;
}
else
{
d;
}
}
else
{
e;
}
a?b:c?d:e?:
if(a)
{
b;
}
else
{
if(c)
{
d;
}
else
{
if(e)
{
}
}
}
17.The output for the first statement is zero.
ReplyDeleteReason: As ‘+’ and ‘–‘ have same precedence the expression will be evaluated from left to right. Initially 1.0+1.0 is evaluated and the result 2.0 is added to 1.0e17. But as 1.0e17 is very large compared to 2.0 the result will be approximated to 1.0e17 from which 1.0e17 is subtracted yielding the result zero.
1.0+1.0 =2.0
2.0+1.0e17 = 1.0e17
1.0e17-1.0e17 = 0
The output for the second statement is 2.0.
Reason: As ‘+’ and ‘–‘ have same precedence the expression will be evaluated from left to right. 1.0e17 is subtracted from 1.0e17 and the result zero is added to 1.0. Then 1.0 is added to 1.0 which gives the answer 2.0.
1.0e17-1.0e17 = 0
0+1.0 = 1.0
1.0+1.0 = 2.0
7.Unions are often nested within a structure that includes a field giving the type of data contained in the union at any particular time. This is an example of a declaration for such a union:
ReplyDeletestruct x
{
int type_tag;
union
{
int x;
float y;
}
}
The most common way of remembering what is in a union is to embed it in a structure with another member of the structure used to indicate the type of thing currently in the union.
It is because of unions that structures cannot be compared for equality. The possibility that a structure might contain a union makes it hard to compare such structures; the compiler can't tell what the union currently contains and so wouldn't know how to compare the structures.
Also structure inside union is used when we need to access the variable of abstract type and also its lower part .
ex: Individual bit of integer and whole integer can be accessed with this.
19. In Java, arrays are first-class objects, while in C++ they are merely a continuous run of their base objects, often referred to using a pointer to their first element and an optional length. In Java, array objects are bounds-checked and contain information about their own length, while in C++ any subsequence can be treated as an array in its own right. Both C++ and Java provide container classes (std::vector and java.util.ArrayList respectively) which are resizable and store their size.In both languages, arrays have a fixed size.
ReplyDeleteUnlike C++, Java provides is a length member, which tells you how big the array is. An exception is thrown if you attempt to access an array out of bounds. All arrays are instantiated in dynamic memory and assignment of one array to another is allowed. However, when you make such an assignment, you simply have two references to the same array. Changing the value of an element in the array using one of the references changes the value insofar as both references are concerned.
declaration of an array of java:
datatype array_name[ ] = new data type[size];
declaration of an array in c++:
datatype array_name[]={1,2,3};
25. Ternary Operator (C/C++):
ReplyDeleteA ternary operator has the following form
exp1 ? exp2 : exp3
The expression exp1 will be evaluated always. Execution of exp2 and exp3 depends on the outcome of exp1. If the outcome of exp1 is non zero exp2 will be evaluated, otherwise exp3 will be evaluated.
Return Type:
The ternary operator has return type. The return type depends on exp2, and convertibility of exp3 into exp2 as per usual\overloaded conversion rules. If they are not convertible, the compiler throws an error.
For example:
The following program compiles without any error. The return type of ternary expression is expected to be float (as that of exp2) and exp3 (i.e. literal zero – int type) is implicitly convertible to float.
#include
using namespace std;
int main()
{
int test = 0;
float fvalue = 3.111f;
cout << (test ? fvalue : 0) << endl;
return 0;
}
The following program will not compile, because the compiler is unable to find return type of ternary expression or implicit conversion is unavailable between exp2 (char array) and exp3 (int).
#include
using namespace std;
int main()
{
int test = 0;
cout << test ? "A String" : 0 << endl;
return 0;
}
We can observe that exp2 is considered as output type and exp3 will be converted into exp2 at runtime. If the conversion is implicit the compiler inserts stubs for conversion. If the conversion is explicit the compiler throws an error. If any compiler misses to catch such error, the program may fail at runtime.