View the step-by-step solution to:

Question

homework in programming language class

Untitled1.png

src="/qa/attachment/10701541/" alt="Untitled.png" />

Untitled.png

SCOPE
public class Main {
public static void main(String args) {
1. Identify the scopes using line numbers of the two x
System. out. print In("Hello world!");
variables declared in this Java code. Is this code legal in
int x = 6;
Java? If legal, write the outputs that the code will print.
sub1( ) ;
B
System. out. printIn("Value of X is"+x);
9
10
11
static void subl (){
12
13
int x = 5;
14
while (x4)i
15
x = 4:
16
x--;
17
System. out. print In("Value of X is "+x) ;
18
SCOPE BINDING
2. An example of nested function is implemented in
SDONGUAWNH
def subi ( ) :
Python.
def sub3 ( ):
a. What will be the value of y in sub2 at line 10?
b. Justify your answer.
x = 3
print ( 'Sub3 x value is' , x)
c. What type of scope binding is this?
d. What is the other type of scope binding?
def sub2() :
e. If a programming language supports the "other
y = x
type" of scope binding, what would be the
10
print ( 'sub2 y value is' , y)
output of this code?
11
12
x = 17
13
14
#call sub3() and sub2() inside sub1( )
15
sub 3 ()
16
sub2 ( )
17
18
# Call sub1( )
19
sub1( )
3. Can you implement nested subprogram definitions shown in Problem 2 in Java, C#, C++? Code
yourself to verify your answer if you are not sure.

Untitled1.png

BLOCKS
#include <stdio. h>
void sub1();
4. Given the C-code, answer the following questions.
a. Is this a legal C code? If legal, what would be the output?
int main(void) {
b. How many times the while loop will run, explain your answer.
printf ("Hello World\");
C. Identify the scope of the two x variables declared in the code.
d. Is declaration of the same variable twice (as we did here) legal in Jav
sub1 ( ) ;
return 0;
10
11
12
void sub1 ( ){
13
14
int x = 5;
while (x>4){
15
int x =0;
16
X--;
17
printf ("Value of X is *d",x) ;
18
19
Referencing Environment
5. Consider the following BLOCKS in a C program:
void fun(void) {
int a, b, c; /* definition 1 */
while (. . .) {
int b, c, d; /*definition 2 */
while (. . .) {
int c, d, e; /* definition 3 */
---- 2
For each of the four marked points in this function, list each visible variable, along with the number of the definition
statement that defines it. Ideally, you need to fill out the table below.
Variable
Point 1
|Point 2
Point 3
Point 4
Point 0
a
Definition
Definition 1
Definition
Undefined
Undefined

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes